functional-programming

how can I pass argument (names) to a function factory?

我的梦境 提交于 2019-12-22 10:59:55
问题 I need to build a lot of functions with lots of different arguments, though they otherwise share a lot of code and structure. To avoid duplication, I thought I'd be clever and build myself a function factory (aka closure). I can't figure out how to pass the function arguments inside the function factory . My use case is a bunch of S3 constructor functions, all of which share the same validation mechanism. So I'll use that as an example to explain my problem. Say, I have a ClassA and ClassB ,

Clojure way of reading large files and transforming data therein

為{幸葍}努か 提交于 2019-12-22 10:56:19
问题 I am processing a Subrip subtitles file which is quite large and need to process it one subtitle at a time. In Java, to extract the subtitles from file, I would write a method with following signature: Iterator<Subtitle> fromSubrip(final Iterator<String> lines); The use of Iterator gives me two benefits: The file is never in the memory in its entirety, nor is any of its transformed stage. An abstraction wherein I can loop over a collection of Subtitle objects without the memory overhead.

In Scala, how would I model a bank account in a stateless, functional manner?

↘锁芯ラ 提交于 2019-12-22 10:49:25
问题 Under an OO paradigm you could have something like class BankAccount(balance: Double) { def deposit(...) def withdraw(...) } I'm wondering how you do the equivalent in the functional paradigm? 回答1: Have each method in the BankAccount class return a new BankAccount object with the new balance. That way, the balance can be an immutable variable. class BankAccount(balance: Double) { def deposit(amount: Double): BankAccount def withdraw(amount: Double): BankAccount } 回答2: Make the account be a

Functional switch case with object literal and Typescript

好久不见. 提交于 2019-12-22 10:39:44
问题 So I've got this classic switch case redux reducer in a todomvc that I want to make functional but can't seem to wrap my head around ts typings for that. Switch case works great for pattern matching and narrows down action discriminated union by type. But I don't seem to get how to pass around narrowed actions with a functional approach where object literal's key should do a type narrowing. What I got so far is union type of all functions and some ts errors by the way. Would really appreciate

How to Implement Map Function in VB.NET

此生再无相见时 提交于 2019-12-22 10:27:36
问题 I am trying to implement Map in VB.NET with the functionality described in this answer. It should take an IEnumerable(Of TIn) and a Function(Of TIn, TOut) , execute the function over every element, and return a new IEnumerable(Of TOut) . I know VB.NET is not a true functional language. I have business requirements, but would still like to use some functional tidbits, especially in conjunction with LINQ. A similar question is asked here, but the question and answer are actually about

How to do Nested For Loops in Functional Style

你。 提交于 2019-12-22 10:16:50
问题 I'm in the process of learning functional programming, and completely getting rid of for loops has been a challenge sometimes, because they provide so much control and freedom. Below is an example of checking if a string is an isogram or not (no letters should be repeated). With nested for loops, it became an easy solution. Is there a way to do this the functional way with any high order functions or anything else? Any suggestion would be a huge help. Code: function isIsogram(string) { let

To prove SKK and II are beta equivalent, lambda calculus

浪尽此生 提交于 2019-12-22 10:12:08
问题 I am new to lambda calculus and struggling to prove the following. SKK and II are beta equivalent. where S = lambda xyz.xz(yz) K = lambda xy.x I = lambda x.x I tried to beta reduce SKK by opening it up, but got nowhere, it becomes to messy. Dont think SKK can be reduced further without expanding S, K. 回答1: SKK = (λxyz.xz(yz))KK → λz.Kz(Kz) (in two steps actually, for the two parameters) Kz = (λxy.x)z → λy.z λz.Kz(Kz) → λz.(λy.z)(λy.z) (again, several steps) → λz.z = I (You should be able to

functional, bind1st and mem_fun

北慕城南 提交于 2019-12-22 09:13:28
问题 Why won't this compile? #include <functional> #include <boost/function.hpp> class A { A() { typedef boost::function<void ()> FunctionCall; FunctionCall f = std::bind1st(std::mem_fun(&A::process), this); } void process() {} }; Errors: In file included from /opt/local/include/gcc44/c++/bits/stl_function.h:712, from /opt/local/include/gcc44/c++/functional:50, from a.cc:1: /opt/local/include/gcc44/c++/backward/binders.h: In instantiation of 'std::binder1st<std::mem_fun_t<void, A> >': a.cc:7:

How can I provide a nice, easy-to-use abstraction for tracking “dirty” elements of a list?

不想你离开。 提交于 2019-12-22 09:01:34
问题 For "fun", and to learn functional programming, I'm developing a program in Clojure that does algorithmic composition using ideas from this theory of music called "Westergaardian Theory". It generates lines of music (where a line is just a single staff consisting of a sequence of notes, each with pitches and durations). It basically works like this: Start with a line consisting of three notes (the specifics of how these are chosen are not important). Randomly perform one of several

Suggestion for solving fragile pattern matching

左心房为你撑大大i 提交于 2019-12-22 08:52:12
问题 I often need to match a tuple of values that should have the same constructor. The catchall _,_ always winds-up at the end. This of course is fragile, any additional constructor added to the type will compile perfectly fine. My current thoughts are to have matches that connect the first but not second argument. But, is there any other options? For example, type data = | States of int array | Chars of (char list) array let median a b = match a,b with | States xs, States ys -> assert( (Array