functional-programming

Can I zip more than two lists together in Scala?

回眸只為那壹抹淺笑 提交于 2019-12-17 08:53:04
问题 Given the following Scala List: val l = List(List("a1", "b1", "c1"), List("a2", "b2", "c2"), List("a3", "b3", "c3")) How can I get: List(("a1", "a2", "a3"), ("b1", "b2", "b3"), ("c1", "c2", "c3")) Since zip can only be used to combine two Lists, I think you would need to iterate/reduce the main List somehow. Not surprisingly, the following doesn't work: scala> l reduceLeft ((a, b) => a zip b) <console>:6: error: type mismatch; found : List[(String, String)] required: List[String] l reduceLeft

Can I zip more than two lists together in Scala?

梦想的初衷 提交于 2019-12-17 08:52:01
问题 Given the following Scala List: val l = List(List("a1", "b1", "c1"), List("a2", "b2", "c2"), List("a3", "b3", "c3")) How can I get: List(("a1", "a2", "a3"), ("b1", "b2", "b3"), ("c1", "c2", "c3")) Since zip can only be used to combine two Lists, I think you would need to iterate/reduce the main List somehow. Not surprisingly, the following doesn't work: scala> l reduceLeft ((a, b) => a zip b) <console>:6: error: type mismatch; found : List[(String, String)] required: List[String] l reduceLeft

Does Java support Currying?

╄→гoц情女王★ 提交于 2019-12-17 08:04:08
问题 I was wondering if there is any way to pull that in Java. I think it is not possible without native support for closures. 回答1: Java 8 (released March 18th 2014) does support currying. The example Java code posted in the answer by missingfaktor can be rewritten as: import java.util.function.*; import static java.lang.System.out; // Tested with JDK 1.8.0-ea-b75 public class CurryingAndPartialFunctionApplication { public static void main(String[] args) { IntBinaryOperator simpleAdd = (a, b) -> a

Using python map and other functional tools

余生长醉 提交于 2019-12-17 08:01:49
问题 This is quite n00bish, but I'm trying to learn/understand functional programming in python. The following code: foos = [1.0,2.0,3.0,4.0,5.0] bars = [1,2,3] def maptest(foo, bar): print foo, bar map(maptest, foos, bars) produces: 1.0 1 2.0 2 3.0 3 4.0 None 5.0 None Q. Is there a way to use map or any other functional tools in python to produce the following without loops etc. 1.0 [1,2,3] 2.0 [1,2,3] 3.0 [1,2,3] 4.0 [1,2,3] 5.0 [1,2,3] Just as a side note how would the implementation change if

Currying a function that takes infinite arguments

故事扮演 提交于 2019-12-17 07:39:33
问题 Using ES5, how do you curry a function that takes infinite arguments. function add(a, b, c) { return a + b + c; } The function above takes only three arguments but we want our curried version to be able to take infinite arguments. Hence, of all the following test cases should pass: var test = add(1); test(2); //should return 3 test(2,3); //should return 6 test(4,5,6); //should return 16 Here is the solution that I came up with: function add(a, b, c) { var args = Array.prototype.slice.call

Is Haskell truly pure (is any language that deals with input and output outside the system)?

女生的网名这么多〃 提交于 2019-12-17 07:12:57
问题 After touching on Monads in respect to functional programming, does the feature actually make a language pure, or is it just another "get out of jail free card" for reasoning of computer systems in the real world, outside of blackboard maths? EDIT: This is not flame bait as someone has said in this post, but a genuine question that I am hoping that someone can shoot me down with and say, proof, it is pure. Also I am looking at the question with respect to other not so pure Functional

“Closures are poor man's objects and vice versa” - What does this mean?

左心房为你撑大大i 提交于 2019-12-17 07:04:30
问题 Closures are poor man's objects and vice versa. I have seen this statement at many places on the web (including SO) but I don't quite understand what it means. Could someone please explain what it exactly means? If possible, please include examples in your answer. 回答1: Objects are poor man's closures. Consider Java. Java is an object-oriented programming language with no language level support for real lexical closures. As a work-around Java programmers use anonymous inner classes that can

“Closures are poor man's objects and vice versa” - What does this mean?

时光怂恿深爱的人放手 提交于 2019-12-17 07:04:14
问题 Closures are poor man's objects and vice versa. I have seen this statement at many places on the web (including SO) but I don't quite understand what it means. Could someone please explain what it exactly means? If possible, please include examples in your answer. 回答1: Objects are poor man's closures. Consider Java. Java is an object-oriented programming language with no language level support for real lexical closures. As a work-around Java programmers use anonymous inner classes that can

What's the Scala way to implement a retry-able call like this one?

纵然是瞬间 提交于 2019-12-17 07:00:17
问题 Still the newbie in Scala and I'm now looking for a way to implement the following code on it: @Override public void store(InputStream source, String destination, long size) { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(size); final PutObjectRequest request = new PutObjectRequest( this.configuration.getBucket(), destination, source, metadata); new RetryableService(3) { @Override public void call() throws Exception { getClient().putObject(request); } }; } What

How to call Python functions dynamically

一世执手 提交于 2019-12-17 06:31:20
问题 I have this code: fields = ['name','email'] def clean_name(): pass def clean_email(): pass How can I call clean_name() and clean_email() dynamically? For example: for field in fields: clean_{field}() I used the curly brackets because it's how I used to do it in PHP but obviously doesn't work. How to do this with Python? 回答1: If don't want to use globals, vars and don't want make a separate module and/or class to encapsulate functions you want to call dynamically, you can call them as the