monads

Monad equivalent in Ruby

∥☆過路亽.° 提交于 2019-12-03 00:53:23
问题 What would an equivalent construct of a monad be in Ruby? 回答1: The precise technical definition : A monad, in Ruby, would be any class with bind and self.unit methods defined such that for all instances m: m.class.unit[a].bind[f] == f[a] m.bind[m.class.unit] == m m.bind[f].bind[g] == m.bind[lambda {|x| f[x].bind[g]}] Some practical examples A very simple example of a monad is the lazy Identity monad, which emulates lazy semantics in Ruby (a strict language): class Id def initialize(lam) @v =

Configuration data in Scala — should I use the Reader monad?

两盒软妹~` 提交于 2019-12-03 00:51:20
问题 How do I create a properly functional configurable object in Scala? I have watched Tony Morris' video on the Reader monad and I'm still unable to connect the dots. I have a hard-coded list of Client objects: class Client(name : String, age : Int){ /* etc */} object Client{ //Horrible! val clients = List(Client("Bob", 20), Client("Cindy", 30)) } I want Client.clients to be determined at runtime, with the flexibility of either reading it from a properties file or from a database. In the Java

Embedding higher kinded types (monads!) into the untyped lambda calculus

↘锁芯ラ 提交于 2019-12-03 00:31:20
It's possible to encode various types in the untyped lambda calculus through higher order functions. Examples: zero = λfx. x one = λfx. fx two = λfx. f(fx) three = λfx. f(f(fx)) etc true = λtf. t false = λtf. f tuple = λxyb. b x y null = λp. p (λxy. false) I was wondering if any research has gone into embedding other less conventional types. It would be brilliant if there is some theorem which asserts that any type can be embedded. Maybe there are restrictions, for example only types of kind * can be embedded. If it is indeed possible to represent less conventional types, it would be awesome

Why does this simple use of the State monad cause a stack overflow?

喜夏-厌秋 提交于 2019-12-02 22:40:45
I was playing around with the State monad, and I don't know what's causing the stack overflow in this simple piece of code. import Control.Monad.State.Lazy tick :: State Int Int tick = do n <- get put $! (n+1) return n million :: Int million = snd $ runState (mapM_ (const tick) [1..1000000]) 0 main = print million Note I would just like to know what's causing the problem in this piece of code, the task itself is not important per se. The problem is that Control.Monad.State.Lazy's (>>=) is so lazy that even the ($!) doesn't help you. Try Control.Monad.State.Strict, that should reach the ($!).

monoid vs monad in Scala

烈酒焚心 提交于 2019-12-02 22:08:20
I have recently tried to find a good source on the difference between monads and monoids. Could someone provide a link to a good resource on this or perhaps take one's time to elaborate on the similarities/differences? Daniel C. Sobral Monads are monoids in the category of endofunctors. Therefore, a monad is just one example of monoid, which is a more general concept. And, though that might be technically true, the most simple answer is that monads and monoids are really nothing like each other, and you shouldn't be trying to learn the difference between them, but just learn them. There's ton

Haskell and random numbers

拈花ヽ惹草 提交于 2019-12-02 21:50:59
I've been messing with Haskell few days and stumbled into a problem. I need a method that returns a random list of integers ( Rand [[Int]] ). So, I defined a type: type Rand a = StdGen -> (a, StdGen) . I was able to produce Rand IO Integer and Rand [IO Integer] ( (returnR lst) :: StdGen -> ([IO Integer], StdGen) ) somehow. Any tips how to produce Rand [[Int]] ? How to avoid the IO depends on why it's being introduced in the first place. While pseudo-random number generators are inherently state-oriented, there's no reason IO needs to be involved. I'm going to take a guess and say that you're

Using Cont to acquire values from the future and the past

旧时模样 提交于 2019-12-02 21:36:00
I'm writing a brainfuck interpreter in Haskell, and I came up with what I believe to be a very interesting description of a program: data Program m = Instruction (m ()) (Program m) | Control (m (Program m)) | Halt However, it's tricky to parse a textual representation of a brainfuck program into this data type. The problem arises with trying to correctly parse square brackets, because there is some knot-tying to do so that the final Instruction inside a loop links to the loop's Control again. A bit more preliminary information. See this version on the github repo for all the details. type

Precise flow control in Haskell

假装没事ソ 提交于 2019-12-02 21:24:03
The Idea Hello! I'm trying to implement in Haskell an image processing library based on dataflow ideology. I've got a problem connected to how I want to handle the flow of control. The main idea is to introduce a time . The time is a Float , which could be accessed anywhere in the code (you can think of it like about State monad, but a little funnier). The funny thing about it, is that we can use timeShift operation on results, affecting the time corresponding operations would see. An example would be best to explain this situation. Lets use following dataflow diagram: -- timeShift(*2) -- -- /

Map and Reduce Monad for Clojure… What about a Juxt Monad?

╄→гoц情女王★ 提交于 2019-12-02 21:20:35
Whilst learning Clojure, I've spent ages trying to make sense of monads - what they are and how we can use them.... with not too much success. However, I found an excellent 'Monads for Dummies' Video Series - http://vimeo.com/20717301 - by Brian Marik for Clojure So far, my understanding of monads is that it is sort of like a macro in that it allows a set of statements to be written in a form that is easy to read - but monads are much more formalised. My observations are limited to two examples: 1. The Identity Monad (or the 'let' monad) taken from http://onclojure.com/2009/03/05/a-monad

Error handling monads in Scala? Try vs Validation

旧街凉风 提交于 2019-12-02 20:33:10
scalaz.Validation is said to be more powerful than the Try monad, because it can accumulate errors. Are there any occasions where you might choose Try over scalaz.Validation or scalaz.\/ ? Travis Brown The most significant argument in favor of Try is that it's in the standard library. It's also used in the standard library—for example the callbacks you register with Future 's onComplete must be functions from Try . It may be used more extensively in the standard library in the future. The fact that it's in the standard library also means it'll look familiar to more people. You'll probably tend