functional-programming

Is the 'Signal' representation of Functional Reactive Programming correct?

那年仲夏 提交于 2019-12-20 10:30:07
问题 I have been researching FRP and found a bunch of different implementations. One model I have seen is one I will refer to as the 'Signal' representation. This essential combines Events and Behaviours into one entity. Firstly, a Signal is an object thats value is a Behaviour. Secondly, a Signal has an Event 'stream' that can be seen and operated on as a standard data structure (you can use 'each', 'map' and 'filter' etc on the Signal to define how Events are reacted to). For example I can do

Why doesn't Java 8's Predicate<T> extend Function<T, Boolean>

丶灬走出姿态 提交于 2019-12-20 10:29:42
问题 If I wrote the Predicate interface, I'd want to encode in the interface the fact that it's just a function that returns a primitive boolean , like this: @FunctionalInterface public interface Predicate<T> extends Function<T, Boolean> { boolean test(T t); @Override default Boolean apply(T t) { return Boolean.valueOf(test(t)); } } I was wondering, is there a compelling reason Java 8 API designers chose to keep the Predicate completely separate from Function ? Is there some evidence that they

How to model class hierarchies in Haskell?

允我心安 提交于 2019-12-20 10:29:20
问题 I am a C# developer. Coming from OO side of the world, I start with thinking in terms of interfaces, classes and type hierarchies. Because of lack of OO in Haskell, sometimes I find myself stuck and I cannot think of a way to model certain problems with Haskell. How to model, in Haskell, real world situations involving class hierarchies such as the one shown here: http://www.braindelay.com/danielbray/endangered-object-oriented-programming/isHierarchy-4.gif 回答1: Let's assume the following

Functional code for looping with early exit

旧街凉风 提交于 2019-12-20 10:29:05
问题 How can I refactor this code in functional style (scala idiomatic) def findFirst[T](objects: List[T]):T = { for (obj <- objects) { if (expensiveFunc(obj) != null) return obj } null.asInstanceOf[T] } 回答1: This is almost exactly what the find method does, except that it returns an Option . So if you want this exact behavior, you can add a call to Option.orNull , like this: objects.find(expensiveFunc).orNull 回答2: First, don't use null in Scala (except when interacting with Java code) but Options

Does the chain function in underscore.js create a monad?

白昼怎懂夜的黑 提交于 2019-12-20 10:00:29
问题 In the chain documentation you find: Calling chain on a wrapped object will cause all future method calls to return wrapped objects as well. When you've finished the computation, use value to retrieve the final value. So does the chain function create a monad? 回答1: No, not a monad, but a comonad! It turns a function that takes a wrapped object and returns a normal value into a function that both takes and returns a wrapped object. As a Haskell type signature that would be: (Wrapped a -> b) ->

What are some good Erlang Primers/Tutorials for beginners?

馋奶兔 提交于 2019-12-20 09:48:40
问题 What are some good links for diving into Erlang and functional programming in general? 回答1: Here's the first half of Concurrent Programming in Erlang [PDF], free. 回答2: this might be a good one to look at http://learnyousomeerlang.com/ 回答3: I think the language itself is surprisingly easy to learn. I recommend http://www.erlang.org/download/getting_started-5.4.pdf 回答4: Let me suggest that Erlang is not necessarily typical of "functional programming in general". If you want a good balance, I'd

What is “Total Functional Programming”?

你。 提交于 2019-12-20 09:39:18
问题 Wikipedia has this to say: Total functional programming (also known as strong functional programming, to be contrasted with ordinary, or weak functional programming) is a programming paradigm which restricts the range of programs to those which are provably terminating. and These restrictions mean that total functional programming is not Turing-complete. However, the set of algorithms which can be used is still huge. For example, any algorithm which has had an asymptotic upper bound

Python recursion challenge [closed]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 09:37:53
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I am currently in an introduction to Python and computational theory class, and there was recently a difficult question on the midterm that I simply was not able to solve. It involves writing code for a program that adds numbers. I believe the question is supposed to use recursion

Haskell tuple constructor (GHC) and the separation between a language and its implementation

无人久伴 提交于 2019-12-20 09:32:35
问题 Haskell blew my mind yet again when I realised that (x,y) Is just syntactic sugar for (,) x y Naturally I wanted to extend this to larger tuples. But (,) x ((,) y z) Gave me (x,(y,z)) Which was not what I was looking for. On a whim, I tried (,,) x y z And it worked, giving exactly what I wanted: (x,y,z) This raised the question: How far can you take it? Much to my astonishment, there seemed to be no limit. All of the below are valid operators: (,) (,,) (,,,) (,,,,) --etc (,,,,,,,,,,,,,,) (,,,

Passing optional callback into Swift function

馋奶兔 提交于 2019-12-20 09:10:24
问题 I'm learning Swift lang, but I cannot pass optional callback argument into function: func dismiss(completion: () -> Void) { if (completion) { return self.dismissViewControllerAnimated(true, completion: completion) } self.dismissModalViewControllerAnimated(true) } This shows me an error - Type () -> Void does not conform to protocol 'LogicValue' Any suggestions? 回答1: Update for Swift 3/4: An optional is no longer a boolean expression, and the deprecated func dismissModalViewControllerAnimated