Python vs Haskell in Monadic Operation

Python Functional programming library: Functools

Let’s explore how to use the functools module in Python to create a simple monadic behavior. While Python doesn’t enforce monads as strictly as Haskell, we can still demonstrate some functional concepts.

  1. Caching with @cache (formerly @lru_cache):
  • The @cache decorator allows you to create a lightweight function cache. It memoizes results, speeding up calculations.
  • Here’s an example using the Fibonacci series: from functools import cache @cache def fibonacci(n: int) -> int: if n <= 1: return n return fibonacci(n - 1) + fibonacci(n - 2) print(fibonacci(10)) # Cached result: 55 print(fibonacci(5)) # Cached result: 5
  • The @cache decorator ensures that previously computed results are reused, avoiding redundant calculations⁴.
  1. Maybe-Like Behavior:
  • While not a strict monad, we can create a simple “Maybe-like” behavior using a custom class: class Maybe: def __init__(self, value): self.value = value def map(self, func): if self.value is not None: return Maybe(func(self.value)) else: return Maybe(None)
  • def add_one(x): return x + 1
  • def double(x): return x * 2
  • maybe_result = Maybe(3).map(add_one).map(double) print(maybe_result.value) # Result: 8
  • maybe_none = Maybe(None).map(add_one).map(double) print(maybe_none.value) # Result: None
  • In this example, the Maybe class wraps a value and applies functions only if the value is not None³.

Remember that Python’s approach to monads is more flexible and pragmatic, but Haskell remains the gold standard for enforcing functional purity and type safety. 🌟🐍🔗

Source: Conversation with Copilot, 6/6/2024
(1) 6 Cool Things You Can Do With The Functools Module. https://pybit.es/articles/6-cool-things-you-can-do-with-the-functools-module/.
(2) ArjanCodes | Python Functors and Monads: A Practical Guide. https://www.arjancodes.com/blog/python-functors-and-monads/.
(3) functools — Higher-order functions and operations on … – Python. https://docs.python.org/3/library/functools.html.
(4) A short, hands-on introduction to Functors and Monads using Python. https://medium.com/@oikonomod/a-short-hands-on-introduction-to-functors-and-monads-using-python-6544b5739f3d.
(5) Functools module in Python – GeeksforGeeks. https://www.geeksforgeeks.org/functools-module-in-python/.

Comonad Use

Certainly! Let’s incorporate a simple example of a comonad in Haskell. While the Maybe monad is more commonly discussed, we can create a comonad-like behavior using the Store Comonad.

  1. Store Comonad:
  • The Store comonad represents a value with an associated context (or “store”). It allows us to extract information from the context.
  • We’ll create a basic Store comonad that holds a value and its index in a list.
  1. Example:

import Control.Comonad -- Define a Store comonad data Store s a = Store [a] s instance Functor (Store s) where fmap f (Store xs s) = Store (fmap f xs) s instance Comonad (Store s) where extract (Store xs s) = xs !! s duplicate (Store xs s) = Store (Store <$> tails xs <*> pure s) s -- Example usage main :: IO () main = do let myList = [10, 20, 30, 40, 50] store = Store myList 2 -- Value 30, index 2 putStrLn $ "Extracted value: " ++ show (extract store) putStrLn $ "Context (duplicated): " ++ show (duplicate store)

  • In this example:
    • We define a Store data type that holds a list of values and an index.
    • The Functor instance applies a function to each value in the list.
    • The Comonad instance provides extract (to get the current value) and duplicate (to create a new Store with all possible contexts).
    • We create a Store with the value 30 (index 2) and demonstrate extraction and duplication.

Remember that comonads are less commonly used than monads, but they provide a dual perspective on context and computation. 🌟🐦🔗 Let me know if you’d like further details or additional examples! 😊

Certainly! Let’s incorporate a simple example of a comonad in Haskell. While the Maybe monad is more commonly discussed, we can create a comonad-like behavior using the Store Comonad.

  1. Store Comonad:
  • The Store comonad represents a value with an associated context (or “store”). It allows us to extract information from the context.
  • We’ll create a basic Store comonad that holds a value and its index in a list.
  1. Example:

import Control.Comonad -- Define a Store comonad data Store s a = Store [a] s instance Functor (Store s) where fmap f (Store xs s) = Store (fmap f xs) s instance Comonad (Store s) where extract (Store xs s) = xs !! s duplicate (Store xs s) = Store (Store <$> tails xs <*> pure s) s -- Example usage main :: IO () main = do let myList = [10, 20, 30, 40, 50] store = Store myList 2 -- Value 30, index 2 putStrLn $ "Extracted value: " ++ show (extract store) putStrLn $ "Context (duplicated): " ++ show (duplicate store)

  • In this example:
    • We define a Store data type that holds a list of values and an index.
    • The Functor instance applies a function to each value in the list.
    • The Comonad instance provides extract (to get the current value) and duplicate (to create a new Store with all possible contexts).
    • We create a Store with the value 30 (index 2) and demonstrate extraction and duplication.

Remember that comonads are less commonly used than monads, but they provide a dual perspective on context and computation. 🌟🐦🔗 Let me know if you’d like further details or additional examples! 😊

Kotlin for Beginners

Most Kotlin tutorial beginners make the mistake of bringing in the legacy Java Object-Oriented thinking into Kotlin — blame the auto-convert feature in Android Studio !

Kotlin, likes Haskell, Scala, has the powerful FP (Functional Programming) as the other side of the dual “FP&OO” features. You can write beautifully re-usuable “bottom-up” software components in FP style, rather than in the GoF pattern adopted by O-O – more lengthy, complex, “top-down”, software components.

1. https://www.udemy.com/kotlin-for-absolute-beginners/

2. https://medium.com/jsguru/the-sensible-ravings-of-a-functional-programming-fanboy-75495985b4d5

3. Android Studio 3.0 https://www.raywenderlich.com/177535/beginning-android-development-with-kotlin-part-two-using-android-studio

4. Data class viewmodel made immutable: https://medium.com/@trionkidnapper/viewmodel-and-kotlin-data-class-7d3a3b854805

5. Explicit / Implicit Receiver : “this“, “superhttps://blog.kotlin-academy.com/programmer-dictionary-implicit-receiver-vs-explicit-receiver-da638de31f3c

6. Six Syntactic Sugars: (sealed class etc) https://medium.com/grand-parade/6-magic-sugars-that-can-make-your-kotlin-codebase-happier-part-1-ceee3c2bc9d3

7. Kotlin a la FP : “maybe” etc

https://blog.plan99.net/kotlin-fp-3bf63a17d64a

8. Kotlin : Nothing Type & Either
https://proandroiddev.com/kotlins-nothing-type-946de7d464fb

9. Complete guide to learn kotlin for android development

https://blog.mindorks.com/a-complete-guide-to-learn-kotlin-for-android-development-b1e5d23cc2d8

10. Try Exception:

https://dzone.com/articles/kotlin-try-type-for-functional-exception-handling

11. Generics: In / Out Type Variants:

https://android.jlelse.eu/in-and-out-type-variant-of-kotlin-587e4fa2944c?gi=1d7e2957c9be