functional-programming

Is Ruby a functional language?

烈酒焚心 提交于 2019-12-20 08:12:36
问题 Wikipedia says Ruby is a functional language, but I'm not convinced. Why or why not? 回答1: I most definitely think you can use functional style in Ruby. One of the most critical aspects to be able to program in a functional style is if the language supports higher order functions... which Ruby does. That said, it's easy to program in Ruby in a non-functional style as well. Another key aspect of functional style is to not have state, and have real mathematical functions that always return the

What are some interesting uses of higher-order functions?

≡放荡痞女 提交于 2019-12-20 08:01:18
问题 I'm currently doing a Functional Programming course and I'm quite amused by the concept of higher-order functions and functions as first class citizens. However, I can't yet think of many practically useful, conceptually amazing, or just plain interesting higher-order functions. (Besides the typical and rather dull map , filter , etc functions). Do you know examples of such interesting functions? Maybe functions that return functions, functions that return lists of functions (?), etc. I'd

How to write lambda methods in Objective-C?

感情迁移 提交于 2019-12-20 07:59:27
问题 How to write lambda methods in Objective-C ? 回答1: The concept of a lambda in Objective-C is now encapsulated with the idea of Blocks which are the equivalent of pass-by-reference functions. Of course, arguably one had that already in C with the idea of function pointers; blocks are just a way of also capturing local state (i.e. can be closures). In fact, blocks can also be used in other C languages as well (on Mac) - there's a proposal to make them part of the standard C syntax. Here's an

Dynamic function return type

隐身守侯 提交于 2019-12-20 07:58:53
问题 I have an app separated on modules. There are several Entities and CSV module. Csv module supports only struct(Entity), but i want to make CSV module works with any type of entity. Now it works like this: Csv module receives data from channel and strictly converts it to EverySize struct. How can I achieve dynamical return type, so it will works with any type of Entity, not only with Everysize func prepareWrapData(data []feed.WrapExporterChannels) []everysize.EverySizeItem { var result [

Using car and cdr

流过昼夜 提交于 2019-12-20 06:28:47
问题 I am new to scheme and having a hard time with using car and cdr. I have an AST string literal in ast. (define ast '(program ((assign (var i int) (call (func getint void int) ())) (assign (var j int) (call (func getint void int) ())) (while (neq (var i int) (var j int)) ((if (gt (var i int) (var j int)) ((assign (var i int) (minus (var i int) (var j int)))) ((assign (var j int) (minus (var j int) (var i int))))))) (call (func putint int void) ((var i int))))) ) I know car returns the head of

Partial sum in Standard ML?

落爺英雄遲暮 提交于 2019-12-20 05:39:18
问题 Im new to functional programming and I have an assignment to compute partial sum of a list. E.g. - psum [1,1,1,1,1]; val it = [1,2,3,4,5] : int list Here is the my code so far. However in function psum2[L] i dont know how to go through each value and add them up so I just print the list. fun psum2(L) : int list = if L=nil then [] else L; fun pSum(L) : int list = psum2(L); exception Empty_List; psum([2,3,4]); 回答1: Your question is a little broad, but here's one way to sum a list. Perhaps you

Understanding Currying in Scala

瘦欲@ 提交于 2019-12-20 05:30:23
问题 I'm getting problems to understand the currying concept, or at least the SCALA currying notation. wikipedia says that currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. Following this explanation, are the two next lines the same for scala? def addCurr(a: String)(b: String): String = {a + " " + b} def add(a:String): String => String = {b => a + " " + b} I've run both lines

Find main diagonal in matrix - Scheme

我是研究僧i 提交于 2019-12-20 05:13:49
问题 I need to extract the main diagonal from a square matrix (1 2 3) (4 5 6) -> (1 5 9) (7 8 9) I have the following code and I need to replace the ... with the appropriate functions. (define (diag m) (if (null? m) '() (cons (... m) (diag (map ... (... m)))))) Input: (diag '((1 2 3) (4 5 6) (7 8 9))) Output: (1 5 9) Any ideas? Thank you! 回答1: So you are asking, given you have the list '((1 2 3) (4 5 6) (7 8 9)) how do I get the value 1 from it? Then you are asking given the same list, how do I

Is RxJava a good fit for branching workflows?

泪湿孤枕 提交于 2019-12-20 05:09:38
问题 I am using RxJava to process some notifications that we pull from a queue. RxJava seemed to work fine with a simple workflow, now with new requirements coming in, the flow is growing in complexity with more branches (please see below picture as a reference) I tried to exemplify the flow with a small unit test: @Test public void test() { Observable.range(1, 100) .groupBy(n -> n % 3) .toMap(GroupedObservable::getKey) .flatMap(m1 -> { Observable<Integer> ones1 = m1.get(0); Observable<Integer>

Using Ramda, and pointfree style, how can I copy the first item of an array to the end of it?

淺唱寂寞╮ 提交于 2019-12-20 04:38:18
问题 I want to take an array [1, 2, 3] and return [1, 2, 3, 1] . I'm using Ramda, and I can get the desired result like this: const fn = arr => R.append(R.prop(0, arr), arr); But I'd like to do it point-free. Here's the closest I've gotten: const fn = R.compose(R.append, R.prop(0)); fn(arr)(arr) But that looks silly. What am I missing? Thanks! 回答1: converge can be very helpful for things like this. const rotate = R.converge(R.append, [R.head, R.identity]) rotate([1, 2, 3]); //=> [1, 2, 3, 1] 回答2: