functional-programming

Scala Catalog of functional Design Patterns

本秂侑毒 提交于 2020-01-12 03:14:11
问题 Since a week I'm reading Programming in Scala. The authors introduce elements of the language step by step , but I'm still confused when to use the functional things like actors, closures, currying,.... I'm looking for a catalog of typical use cases or best practices for functional contructs. I don't mean reimplementing well known pattern like GoF in Scala like http://github.com/greedy/scala-study-group/tree/master/patterns/ 回答1: Being confused about when to use closures or currying is like

Scala Catalog of functional Design Patterns

让人想犯罪 __ 提交于 2020-01-12 03:14:06
问题 Since a week I'm reading Programming in Scala. The authors introduce elements of the language step by step , but I'm still confused when to use the functional things like actors, closures, currying,.... I'm looking for a catalog of typical use cases or best practices for functional contructs. I don't mean reimplementing well known pattern like GoF in Scala like http://github.com/greedy/scala-study-group/tree/master/patterns/ 回答1: Being confused about when to use closures or currying is like

F# and ADO.NET - idiomatic F#

為{幸葍}努か 提交于 2020-01-12 02:43:10
问题 I'm just starting to learn F#. I wrote this F#/ADO.NET code last night. In what ways would you improve the syntax - make it feel like idiomatic F#? let cn = new OleDbConnection(cnstr) let sql = "SELECT * FROM People" let da = new OleDbDataAdapter(new OleDbCommand(sql, cn)) let ds = new DataSet() cn.Open() let i = da.Fill(ds) let rowCol = ds.Tables.[0].Rows let rowCount = rowCol.Count printfn "%A" rowCount for i in 0 .. (rowCount - 1) do let row:DataRow = rowCol.[i] printfn "%A" row.["LastName

Church encoding of lists using right folds and difference lists

点点圈 提交于 2020-01-11 13:08:45
问题 Here is the sequential question after How to store data of a functional chain of Monoidal List? and Extracting data from a function chain without arrays and here I would like to express my respect and appreciation for contributors to my Questions, especially by @Aadit M Shah and @user633183 Now, this question is opened to clarify the similarities and differences or relation between Difference list and Church list . Difference list https://stackoverflow.com/a/51320041/6440264 A difference list

PL/SQL passing functions as parameters

孤街醉人 提交于 2020-01-11 09:33:14
问题 I've programmed in PL/SQL during half an year and I had the impression it's a quite plain programming language (IMHO). Although I've stumbled upon interesting articles, like this one - Design Patterns in PL/SQL – Interface Injection for even looser coupling, I recommend reading. Talking about dependency injection, I miss an special feature: passing subroutines as parameters. Is it possible? How? For instance, imagine I have a code like this in javascript: function tell_me (printer) { printer

Functions that look pure to callers but internally use mutation

ぐ巨炮叔叔 提交于 2020-01-11 08:27:09
问题 I just got my copy of Expert F# 2.0 and came across this statement, which somewhat surprised me: For example, when necessary, you can use side effects on private data structures allocated at the start of an algorithm and then discard these data structures before returning a result; the overall result is then effectively a side-effect-free function. One example of separation from the F# library is the library's implementation of List.map, which uses mutation internally; the writes occur on an

Java 8 function that always return the same value without regarding to parameter

☆樱花仙子☆ 提交于 2020-01-11 08:17:54
问题 Is there a predefined Function in Java 8 that does something like this: static <T, R> Function<T, R> constant(R val) { return (T t) -> { return val; }; } To answer people's query on why I need this function here is the real usage when I am trying to parse an integer to an roman numerals: // returns the stream of roman numeral symbol based // on the digit (n) and the exponent (of 10) private static Stream<Symbol> parseDigit(int n, int exp) { if (n < 1) return Stream.empty(); Symbol base =

Collect Lines using Multimap Collector

喜欢而已 提交于 2020-01-11 05:22:10
问题 Is there a way to covert the below to using collectors yet? List<String[]> lines = getLines(); Multimap<String,String> multimap = ArrayListMultimap.create(); lines.forEach(line -> multimap.put(line[0],line[1]); ); 回答1: You can use Multimaps.toMultimap collector: ListMultimap<String, String> multimap = lines.stream() .collect(Multimaps.toMultimap( l -> l[0], l -> l[1], ArrayListMultimap::create )); Or if you don't need mutability, use ImmutableListMultimap.toImmutableListMultimap collector:

What is the general case of QuickCheck's promote function?

Deadly 提交于 2020-01-11 04:50:06
问题 What is the general term for a functor with a structure resembling QuickCheck's promote function, i.e., a function of the form: promote :: (a -> f b) -> f (a -> b) (this is the inverse of flip $ fmap (flip ($)) :: f (a -> b) -> (a -> f b) ). Are there even any functors with such an operation, other than (->) r and Id ? (I'm sure there must be). Googling 'quickcheck promote' only turned up the QuickCheck documentation, which doesn't give promote in any more general context AFAICS; searching SO

The Seasoned Schemer, letcc and guile

≡放荡痞女 提交于 2020-01-10 19:16:12
问题 A few questions here, regarding letcc that is used in The Seasoned Schemer. (define (intersect-all sets) (letcc hop (letrec ((A (lambda (sets) (cond ((null? (car sets)) (hop '()) ((null? (cdr sets)) (car sets)) (else (intersect (car sets) (A (cdr sets))))))) ; definition of intersect removed for brevity (cond ((null? sets) '()) (else (A sets)))))) I think I understand what letcc achieves, and that is basically something like catch and throw in ruby (and seemingly CL), which basically means a