functional-programming

Mixing object-oriented and functional programming

与世无争的帅哥 提交于 2019-12-29 16:26:41
问题 What languages are available that promote both object-oriented and functional programming? I know that any language that supports first-class functions can be considered functional, but I'm looking for a syntax that's specifically targeted for both coding styles. Using such a language, I'm imagining isolating all state changes to a single portion of code and having the rest of the program be purely functional. Just thinking of it makes me drool (debugging heaven!). So far I've discovered

Algorithm to create fair / evenly matched teams based on player rankings

牧云@^-^@ 提交于 2019-12-29 14:33:02
问题 I have a data set of players' skill ranking, age and sex and would like to create evenly matched teams. Teams will have the same number of players (currently 8 teams of 12 players). Teams should have the same or similar male to female ratio. Teams should have similar age curve/distribution. I would like to try this in Haskell but the choice of coding language is the least important aspect of this problem. 回答1: This is a bin packing problem, or a multi-dimensional knapsack problem. Björn B.

Algorithm to create fair / evenly matched teams based on player rankings

我与影子孤独终老i 提交于 2019-12-29 14:30:32
问题 I have a data set of players' skill ranking, age and sex and would like to create evenly matched teams. Teams will have the same number of players (currently 8 teams of 12 players). Teams should have the same or similar male to female ratio. Teams should have similar age curve/distribution. I would like to try this in Haskell but the choice of coding language is the least important aspect of this problem. 回答1: This is a bin packing problem, or a multi-dimensional knapsack problem. Björn B.

Substitute values in a string with placeholders in Scala

半腔热情 提交于 2019-12-29 14:21:54
问题 I have just started using Scala and wish to better understand the functional approach to problem solving. I have pairs of strings the first has placeholders for parameter and it's pair has the values to substitute. e.g. "select col1 from tab1 where id > $1 and name like $2" "parameters: $1 = '250', $2 = 'some%'" There may be many more than 2 parameters. I can build the correct string by stepping through and using regex.findAllIn(line) on each line and then going through the iterators to

Truly understanding the difference between procedural and functional

夙愿已清 提交于 2019-12-29 10:08:59
问题 I'm really having a hard time understanding the difference between procedural and functional programming paradigms. Here are the first two paragraphs from the Wikipedia entry on functional programming : In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in

Coolest C# LINQ/Lambdas trick you've ever pulled?

为君一笑 提交于 2019-12-29 10:06:16
问题 Saw a post about hidden features in C# but not a lot of people have written linq/lambdas example so... I wonder... What's the coolest (as in the most elegant) use of the C# LINQ and/or Lambdas/anonymous delegates you have ever saw/written? Bonus if it has went into production too! 回答1: The LINQ Raytracer certainly tops my list =) I'm not quite sure if qualifies as elegant but it is most certainly the coolest linq-expression I've ever seen! Oh, and just to be extremely clear; I did not write

difference between foldLeft and reduceLeft in Scala

风流意气都作罢 提交于 2019-12-29 10:05:25
问题 I have learned the basic difference between foldLeft and reduceLeft foldLeft: initial value has to be passed reduceLeft: takes first element of the collection as initial value throws exception if collection is empty Is there any other difference ? Any specific reason to have two methods with similar functionality? 回答1: Few things to mention here, before giving the actual answer: Your question doesn't have anything to do with left , it's rather about the difference between reducing and folding

Explanation of lists:fold function

筅森魡賤 提交于 2019-12-29 09:05:28
问题 I learning more and more about Erlang language and have recently faced some problem. I read about foldl(Fun, Acc0, List) -> Acc1 function. I used learnyousomeerlang.com tutorial and there was an example (example is about Reverse Polish Notation Calculator in Erlang): %function that deletes all whitspaces and also execute rpn(L) when is_list(L) -> [Res] = lists:foldl(fun rpn/2, [], string:tokens(L," ")), Res. %function that converts string to integer or floating poitn value read(N) -> case

Explanation of lists:fold function

放肆的年华 提交于 2019-12-29 09:05:13
问题 I learning more and more about Erlang language and have recently faced some problem. I read about foldl(Fun, Acc0, List) -> Acc1 function. I used learnyousomeerlang.com tutorial and there was an example (example is about Reverse Polish Notation Calculator in Erlang): %function that deletes all whitspaces and also execute rpn(L) when is_list(L) -> [Res] = lists:foldl(fun rpn/2, [], string:tokens(L," ")), Res. %function that converts string to integer or floating poitn value read(N) -> case

Do-while loop in Clojure?

淺唱寂寞╮ 提交于 2019-12-29 08:38:05
问题 So I want to first execute a bunch of code, and then ask the user if he wants to do that again. I thought the most convenient way to do this would be a do-while loop like in C++, and since I couldn't seem to find any do-while functions in Clojure, I wrote the following: (defmacro do-while "Executes body before testing for truth expression" [test & body] `(do (do ~@body) (while ~test ~@body))) Would there be a better (as in more idiomatic Clojure-ish) way of writing this macro, or perhaps a