functional-programming

Implementation of flatMap() for State transition

青春壹個敷衍的年華 提交于 2019-12-23 12:17:06
问题 Exercise 6.8, Chiusano and Bjarnason, Functional Programming in Scala , p. 87 asks how one might implement flatMap() for the following trait: trait RNG { def nextInt: (Int, RNG) } type Rand[+A] = RNG => (A, RNG) The answer key gives the following solution: def flatMap[A,B](f: Rand[A])(g: A => Rand[B]): Rand[B] = rng => { val (a, r1) = f(rng) g(a)(r1) // We pass the new state along } Stackoverflow provides many answers to flatMap()/monad questions, but none which for me answered my questions

Javascript: why the access to closure variable might be slow

爱⌒轻易说出口 提交于 2019-12-23 12:16:58
问题 Recently I've read this performance guide Let's make the web faster and was puzzled by "Avoiding pitfalls with closures" recommendations (as if these advices were given for CommonLisp users where variable scoping is dynamic): var a = 'a'; function createFunctionWithClosure() { var b = 'b'; return function () { var c = 'c'; a; b; c; }; } var f = createFunctionWithClosure(); f(); when f is invoked, referencing a is slower than referencing b , which is slower than referencing c . It's quite

How to find the mode value of a list?

我是研究僧i 提交于 2019-12-23 11:57:11
问题 Is there a function in scala collections to find the max number of occurrence of a value in a list, Lets say I have a list L = List("A","B","B","E","B","E","B","B","C","E","B") output: "B". I can write a module to calculate this, but I would expect there should be a scala "way" or scala collection function to do this, already. Thanks! 回答1: I don't know of a ready-made way to do it, but this is how I would do it: l.groupBy(i => i).mapValues(_.size).maxBy(_._2)._1 Oh, but note this doesn't

Thinking Functionally. Building a New Array in Haskell / Purescript

微笑、不失礼 提交于 2019-12-23 10:40:37
问题 I'm new to functional programming, and I've decided to build an app in Purescript. I've hit my first hurdle, and I'm not sure how to think about this conceptually. I'm not looking for code as much as a way to think functionally about this problem. I have a list of data. Specifically, something like [ {a :: String, b :: String, c :: String} ] I would like to create a list of Html (which is a purescript-halogen type) by using the record provided (with a list of the above types). So, I would

Pattern matching zero-argument functions in scala: mystified by warning

删除回忆录丶 提交于 2019-12-23 10:28:15
问题 I'm playing with scala's distributed actors. Very nice. I have a server which executes incoming function objects. For example, the client has object Tasks { def foo = {Console.println("I am Foo")}; def bar = {Console.println("I am Bar");} } // In client actor... ... server ! Tasks.foo _ ... And the server can pick these up and execute them with actor code like react { case task:(()=>Unit) => task() This all works nicely (which is very very cool indeed) but I'm mystified by a warning message

Compose multiple predicate functions into one

杀马特。学长 韩版系。学妹 提交于 2019-12-23 10:06:41
问题 Is it possible to compose for example: (defn- multiple-of-three? [n] (zero? (mod n 3)) (defn- multiple-of-five? [n] (zero? (mod n 5)) into: multiple-of-three-or-five? so I can use it for filtering: (defn sum-of-multiples [n] (->> (range 1 n) (filter multiple-of-three-or-five?) (reduce +))) Also I don't want to define it like this: (defn- multiple-of-three-or-five? [n] (or (multiple-of-three? n) (multiple-of-five? n))) For example with Javascript module Ramda it would be achieved as: http:/

How applying a function to a variant?

妖精的绣舞 提交于 2019-12-23 09:57:34
问题 Let this types = type intC = int;; type boolC = bool; type stringC = string;; type component = A of intC | B of boolC | C of stringC;; If I want to apply a function on the type a of a component A, do I need systematically to deconstruct the component ? for exemple do i have to do : let add comp = match comp with | A i -> Some (i + 2) (*only A interests me, I return i + 2*) | _ -> None (*otherwise I return nothing*) and then for any function on a component A ? Is there any mean to avoid thy

Why is x = x +1 valid in Elixir?

萝らか妹 提交于 2019-12-23 09:56:43
问题 Everything I've read about Elixir says that assignment should be thought of as pattern matching. If so then why does x = x + 1 work in Elixir? There is no value of x for which x = x + 1. 回答1: Everything I've read about Elixir says that assignment should be thought of as pattern matching. In Elixir, = is called the pattern match operator, but it does not work the same way as the pattern match operator in Erlang. That's because in Elixir variables are not single assignment like they are in

Scala, Actors, what happens to unread inbox messages?

独自空忆成欢 提交于 2019-12-23 09:40:56
问题 What happens to unread inbox messages in Scala Actors ? For example two cases: 1.If forget to implement react case for special message: actor!NoReactCaseMessage 2. If messages comes too fast: (timeOfProcessingMessage > timeOfMessageComes) If first or second case happens, would it be stacked in memory? EDIT 1 Is there any mechanism to see this type of memory leak is happening? Maybe, control number of unread messages then make some garbage collect or increase actor pool. How to get number of

Haskell - have a function return an empty character

旧时模样 提交于 2019-12-23 09:32:25
问题 I'm trying to create a function that drops every n'th element from a string. dropEvery :: String -> Int -> String dropEvery str n = map (\(char, indx) -> if indx `mod` n /= 0 then char else ' ') (zip str [1..]) Right now it simply replaces every n'th element with a space, but what am I supposed to put after the "else" if I want it to return an "empty char". I understand that such a thing doesn't exist in Haskell so the question is - how am I supposed to tell Haskell to not return anything and