functional-programming

An implementation problem of F# Seq

旧巷老猫 提交于 2020-01-01 04:29:13
问题 I am digging into F# source code recently. in Seq.fs: // Binding. // // We use a type defintion to apply a local dynamic optimization. // We automatically right-associate binding, i.e. push the continuations to the right. // That is, bindG (bindG G1 cont1) cont2 --> bindG G1 (cont1 o cont2) // This makes constructs such as the following linear rather than quadratic: // // let rec rwalk n = { if n > 0 then // yield! rwalk (n-1) // yield n } After seeing the above code, I tested two code: let

How to interleave (merge) two Java 8 Streams?

こ雲淡風輕ζ 提交于 2020-01-01 04:26:14
问题 Stream<String> a = Stream.of("one", "three", "five"); Stream<String> b = Stream.of("two", "four", "six"); What do I need to do for the output to be the below? // one // two // three // four // five // six I looked into concat but as the javadoc explains, it just appends one after the other, it does not interleave / intersperse. Stream<String> out = Stream.concat(a, b); out.forEach(System.out::println); Creates a lazily concatenated stream whose elements are all the elements of the first

Mix and match stateful computations within the State monad

独自空忆成欢 提交于 2020-01-01 04:15:06
问题 The state of my program consists of three values, a , b , and c , of types A , B , and C . Different functions need access to different values. I want to write functions using the State monad so that each function can only access the pieces of the state that it needs to access. I have four functions of the following types: f :: State (A, B, C) x g :: y -> State (A, B) x h :: y -> State (B, C) x i :: y -> State (A, C) x Here is how I call g within f : f = do -- some stuff -- y is bound to an

Scala String Equality Question from Programming Interview

我怕爱的太早我们不能终老 提交于 2020-01-01 04:02:10
问题 Since I liked programming in Scala, for my Google interview, I asked them to give me a Scala / functional programming style question. The Scala functional style question that I got was as follows: You have two strings consisting of alphabetic characters as well as a special character representing the backspace symbol. Let's call this backspace character '/'. When you get to the keyboard, you type this sequence of characters, including the backspace/delete character. The solution you are to

Cartesian product traverse in scalaz

馋奶兔 提交于 2020-01-01 03:38:24
问题 In Eric Torreborre's blogpost on the paper Essence of the Iterator Pattern, he describes how the cartesian product of a traverse is also a traverse. Can anyone show me an example of this using the scalaz library as I can't figure it out. Let's say the problem is that, for a List[Int] I want to provide both of: The Int sum of the elements in the list A List[String] the elements of which are created by appending the "Z" to the String representation of the Int s My understanding is that I can do

Functional composition with variadic templates in C++11

我的未来我决定 提交于 2020-01-01 01:11:11
问题 I'm a mathematician used to doing "old style" C++ programming for a long time now. I feel that some new syntactic constructions offerred by C++11 could help me achieve some better code regarding my professionnal projects. Yet as I'm no CS professionnal I must confess that I lack the knowledge to understand some examples I encounter in my self-learning process, altough I've been pretty lucky/succesful so far. My impression is that variadic templates can be used to implement type-safe functions

Apply a list of Python functions in order elegantly

戏子无情 提交于 2019-12-31 22:26:12
问题 I have an input value val and a list of functions to be applied in the order: funcs = [f1, f2, f3, ..., fn] How to apply elegantly and not writing fn( ... (f3(f2(f1(val))) ... ) and also not using for loop: tmp = val for f in funcs: tmp = f(tmp) Thanks Martijn for the awesome answer. There's some reading I found: https://mathieularose.com/function-composition-in-python/ . 回答1: Use the reduce() function: # forward-compatible import from functools import reduce result = reduce(lambda res, f: f

Machine model for functional programming

风格不统一 提交于 2019-12-31 22:26:11
问题 I've read somewhere that the current machine model is not quite fit for functional programming. So what is the limitations of the current machine model? Has a more suitable machine model been developed? 回答1: Landin's SECD machine was one of the first designed to evaluate lambda calculus expressions. Ager et al. cover other abstract and virtual machines designed to do same in their paper "A Functional Correspondence between Evaluators and Abstract Machines". 回答2: Yes, it was developed. Here's

Map list onto dictionary

删除回忆录丶 提交于 2019-12-31 17:54:34
问题 Is there a way to map a list onto a dictionary? What I want to do is give it a function that will return the name of a key, and the value will be the original value. For example; somefunction(lambda a: a[0], ["hello", "world"]) => {"h":"hello", "w":"world"} (This isn't a specific example that I want to do, I want a generic function like map() that can do this) 回答1: I don't think a standard function exists that does exactly that, but it's very easy to construct one using the dict builtin and a

r data.table functional programming / metaprogramming / computing on the language

*爱你&永不变心* 提交于 2019-12-31 16:47:47
问题 I am exploring different ways to wrap an aggregation function (but really it could be any type of function) using data.table (one dplyr example is also provided) and was wondering on best practices for functional programming / metaprogramming with respect to performance (does the implementation matter with respect to potential optimization that data.table may apply) readability (is there a commonly agreed standard e.g. in most packages utilizing data.table) ease of generalization (are there