functional-programming

Using Function.prototype.bind with an array of arguments?

两盒软妹~` 提交于 2019-12-17 17:38:44
问题 How can I call Function.prototype.bind with an array of arguments, as opposed to hardcoded arguments? (Not using ECMA6, so no spread operator). I'm trying to put a promises wrapper around a module that uses callbacks and I want to bind all of the arguments passed in to my wrapper method and bind them. Then I want to call the partially applied bound function with my own callback, which will resolve or reject a promise. var find = function() { var deferred, bound; deferred = Q.defer(); bound =

How to implement a stack-safe chainRec operator for the continuation monad?

耗尽温柔 提交于 2019-12-17 17:05:46
问题 I am currently experimenting with the continuation monad. Cont is actually useful in Javascript, because it abstracts from the callback pattern. When we deal with monadic recursion, there is always the risk of a stack overflow, because the recursive call isn't in tail position: const chain = g => f => k => g(x => f(x) (k)); const of = x => k => k(x); const id = x => x; const inc = x => x + 1; const repeat = n => f => x => n === 0 ? of(x) : chain(of(f(x))) (repeat(n - 1) (f)); console.log(

Function Application Operator ($) in F#?

て烟熏妆下的殇ゞ 提交于 2019-12-17 16:55:19
问题 Let's say I have this code let identifier = spaces_surrounded (many1Satisfy isLetter) I was wondering if it there was any native F# function that allowed me to refactor it to let identifier = spaces_surrounded $ many1Satisfy isLetter that is, something such as let ($) f1 f2 = f1 (f2) (that is if I am not mistaken, my Haskell skills are not too sharp..). 回答1: The standard F# idiom for this is the forward pipe operator |> were you would rewrite let identifier = spaces_surrounded (many1Satisfy

Assisting Agda's termination checker

余生颓废 提交于 2019-12-17 15:55:19
问题 Suppose we define a function f : N \to N f 0 = 0 f (s n) = f (n/2) -- this / operator is implemented as floored division. Agda will paint f in salmon because it cannot tell if n/2 is smaller than n. I don't know how to tell Agda's termination checker anything. I see in the standard library they have a floored division by 2 and a proof that n/2 < n. However, I still fail to see how to get the termination checker to realize that recursion has been made on a smaller subproblem. 回答1: Agda's

Why can't I map integers to strings when streaming from an array?

荒凉一梦 提交于 2019-12-17 15:35:36
问题 This code works (taken in the Javadoc): List<Integer> numbers = Arrays.asList(1, 2, 3, 4); String commaSeparatedNumbers = numbers.stream() .map(i -> i.toString()) .collect(Collectors.joining(", ")); This one can't be compiled: int[] numbers = {1, 2, 3, 4}; String commaSeparatedNumbers = Arrays.stream(numbers) .map((Integer i) -> i.toString()) .collect(Collectors.joining(", ")); IDEA tells me I have an "incompatible return type String in lambda expression". Why ? And how to fix that ? 回答1:

Is this property of a functor stronger than a monad?

一个人想着一个人 提交于 2019-12-17 15:34:33
问题 While thinking about how to generalize monads, I came up with the following property of a functor F: inject :: (a -> F b) -> F(a -> b) -- which should be a natural transformation in both a and b. In absence of a better name, I call the functor F bindable if there exists a natural transformation inject shown above. The main question is, whether this property is already known and has a name, and how is it related to other well-known properties of functors (such as, being applicative, monadic,

Why is appending to a list bad?

喜你入骨 提交于 2019-12-17 15:24:02
问题 I've recently started learning scala, and I've come across the :: (cons) function, which prepends to a list. In the book "Programming in Scala" it states that there is no append function because appending to a list has performance o(n) whereas prepending has a performance of o(1) Something just strikes me as wrong about that statement. Isn't performance dependent on implementation? Isn't it possible to simply implement the list with both forward and backward links and store the first and last

Implementing a callback in Python - passing a callable reference to the current function

寵の児 提交于 2019-12-17 15:23:27
问题 I want to implement the Observable pattern in Python for a couple of workers, and came across this helpful snippet: class Event(object): pass class Observable(object): def __init__(self): self.callbacks = [] def subscribe(self, callback): self.callbacks.append(callback) def fire(self, **attrs): e = Event() e.source = self for k, v in attrs.iteritems(): setattr(e, k, v) for fn in self.callbacks: fn(e) Source: Here As i understand it, in order to subscribe , I would need to pass a callback to

Tail-recursion on trees

左心房为你撑大大i 提交于 2019-12-17 13:27:13
问题 I have a data structure, datatype 'a tree = Leaf | Branch of 'a tree * 'a * 'a tree and I want to write a function that traverses this tree in some order. It doesn't matter what it does, so it could be a treefold : ('a * 'b -> 'b) -> 'b -> 'a tree -> 'b . I can write this function like this: fun treefold f acc1 Leaf = acc1 | treefold f acc1 (Branch (left, a, right)) = let val acc2 = treefold acc1 left val acc3 = f (a, acc2) val acc4 = treefold acc3 right in acc4 end But because I inevitably

What is the added value of the kestrel functional programming Design Pattern? (Scala)

回眸只為那壹抹淺笑 提交于 2019-12-17 12:31:12
问题 I'm wondering why is it so useful to put away the side effect part of a code using Kestrel. Does anyone has experience with it? and can explain the real motivation and how does it help exactly. Although i understand that Pure functional programing is about zero-side effect and henceforth better debugging and predictability of the code. However in the case of Kestrel, I don't see how it really help to do that ? Best, MM- 回答1: The point is to avoid creating an intermediate variable that you