functional-programming

How do you curry the 2nd (or 3rd, 4th, …) parameter in F# or any functional language?

房东的猫 提交于 2020-01-12 13:47:36
问题 I'm just starting up with F# and see how you can use currying to pre-load the 1st parameter to a function. But how would one do it with the 2nd, 3rd, or whatever other parameter? Would named parameters to make this easier? Are there any other functional languages that have named parameters or some other way to make currying indifferent to parameter-order? 回答1: Typically you just use a lambda: fun x y z -> f x y 42 is a function like 'f' but with the third parameter bound to 42. You can also

Map values in Collectors.groupingBy()

旧城冷巷雨未停 提交于 2020-01-12 13:43:08
问题 For the sake of this example, let's assume I have a simple type Tuple with two attributes: interface Tuple<T, U> { T getFirst(); U getSecond(); } Now I want to transform a collection of (first, second) tuples into a map which maps each first value to a set of all second values contained in tuples with that specific first value. The method groupSecondByFirst() shows a possible implementation doing what I want: <T, U> Map<T, Set<U>> groupSecondByFirst(Set<Tuple<T, U>> tuples) { Map<T, Set<U>>

Map values in Collectors.groupingBy()

只愿长相守 提交于 2020-01-12 13:42:49
问题 For the sake of this example, let's assume I have a simple type Tuple with two attributes: interface Tuple<T, U> { T getFirst(); U getSecond(); } Now I want to transform a collection of (first, second) tuples into a map which maps each first value to a set of all second values contained in tuples with that specific first value. The method groupSecondByFirst() shows a possible implementation doing what I want: <T, U> Map<T, Set<U>> groupSecondByFirst(Set<Tuple<T, U>> tuples) { Map<T, Set<U>>

Confused about diagrams of Yampa switches

↘锁芯ラ 提交于 2020-01-12 09:53:07
问题 There is some diagrams of Yampa switches at: http://www.haskell.org/haskellwiki/Yampa/switch http://www.haskell.org/haskellwiki/Yampa/rSwitch http://www.haskell.org/haskellwiki/Yampa/kSwitch (and so on). I've found that the switch , the only one diagram with description, is the easiest one to get understand. The others seems hard to follow the similar symbols to read the diagrams. For example, to try to read the rSwitch with the symbols used in the switch may be: Be a recursive SF which is

Functional way to implement a thread safe shared counter

一世执手 提交于 2020-01-12 08:02:11
问题 I'm relatively new to Scala and functional programming, and I like the idea that using immutable objects I can avoid many thread safety pitfalls. One thing still haunts me, and it's the classical example used to teach thread safety - the shared counter. I was wondering if it would be possible to implement a thread-safe counter (a request counter in this example), using immutable objects, and functional concepts, and avoid synchronization completely. So for reference here are first the

Typed FP: Tuple Arguments and Curriable Arguments

我的未来我决定 提交于 2020-01-12 07:35:10
问题 In statically typed functional programming languages, like Standard ML, F#, OCaml and Haskell, a function will usually be written with the parameters separated from each other and from the function name simply by whitespace: let add a b = a + b The type here being " int -> (int -> int) ", i.e. a function that takes an int and returns a function which its turn takes and int and which finally returns an int. Thus currying becomes possible. It's also possible to define a similar function that

How does pattern matching work behind the scenes in F#?

∥☆過路亽.° 提交于 2020-01-12 06:41:22
问题 I am completely new to F# (and functional programming in general) but I see pattern matching used everywhere in sample code. I am wondering for example how pattern matching actually works? For example, I imagine it working the same as a for loop in other languages and checking for matches on each item in a collection. This is probably far from correct, how does it actually work behind the scenes? 回答1: It depends on what kind of pattern matching do you mean - it is quite powerful construct and

About the pattern matching algorithm in OCaml

点点圈 提交于 2020-01-12 05:42:10
问题 I am writing a compiler for a functional language I designed with OCaml. I want my little language to have the feature of pattern matching, however, I got stuck in coming up with an algorithm to implement it. It seems really complicated as I dig into the problem. I can't find much useful information about the corresponding algorithm with google. I will be appreciated if someone can give me some hint or point me to the resources. Or are there any tricks to take advantage of OCaml's power in

Replacing Exceptions With Either/Maybe/Option

 ̄綄美尐妖づ 提交于 2020-01-12 03:52:50
问题 I came across this dead end while trying to replace exceptions with either monad in c#. Which leads me to think maybe it is not only language specific problem and more technique related missing feature. Let me try to re-explain it more globally: Given: I have a 3rd party function( a function that is imported into my code and I have no access to) which receives a lazy list (c# IEnumerable,f# Seq...) and consume it I Want: To apply a function (LINQ select,map...) on the method's lazy list

Is there a functional language for C++ ecosystem?

心不动则不痛 提交于 2020-01-12 03:14:21
问题 Java has Scala and .NET has F#. Both of these languages are very highly integrated into the respective Java and .NET platforms. Classes can be written in Scala then extended in Java for example. Does there exist an equivalent functional language that interoperates highly with C++? 回答1: The Felix language by John Skaller is designed to interoperate with C++ and provide the functional paradigm. There are problems with doing this though. Functional languages provide first-class functions which