functional-programming

What kinds of functions are considered as “composable”?

喜欢而已 提交于 2020-01-02 06:15:10
问题 The Wikipedia article Function composition (computer science) says: Like the usual composition of functions in mathematics, the result of each function is passed as the argument of the next, and the result of the last one is the result of the whole. I have two questions about it: A composable function must have both arguments and return value? So following functions are not: def doNothing(): Unit = () def myName(): String = "My name" def eat(food:String): Unit = () Is my understanding correct

Scala Infinite Iterator OutOfMemory

落花浮王杯 提交于 2020-01-02 05:33:08
问题 I'm playing around with Scala's lazy iterators, and I've run into an issue. What I'm trying to do is read in a large file, do a transformation, and then write out the result: object FileProcessor { def main(args: Array[String]) { val inSource = Source.fromFile("in.txt") val outSource = new PrintWriter("out.txt") try { // this "basic" lazy iterator works fine // val iterator = inSource.getLines // ...but this one, which incorporates my process method, // throws OutOfMemoryExceptions val

How to make Scala's immutable collections hold immutable objects

别说谁变了你拦得住时间么 提交于 2020-01-02 03:54:06
问题 I'm evaluating Scala and am having a problem with its immutable collections. I want to make immutable collections, which are completely immutable, right down through all the contained objects, the objects they reference, ad infinitum. Is there a simple way to do this? The code on http://www.finalcog.com/immutable-containers-scala illustrates what I'm trying to achieve, and a nasty work around (ImmutablePoint). The problem with the workaround is that every time I want to change an object I

Is there any difference between closure in Scheme and usual closure in other languages?

僤鯓⒐⒋嵵緔 提交于 2020-01-02 03:53:06
问题 I'm studying SICP right now. And I found the definition of closure in SICP is (maybe) different from closure definition in other languages. Here's what SICP says: The ability to create pairs whose elements are pairs is the essence of list structure's importance as a representational tool. We refer to this ability as the closure property of cons. In general, an operation for combining data objects satisfies the closure property if the results of combining things with that operation can

When to use interfaces, and when to use higher order functions?

我是研究僧i 提交于 2020-01-02 00:59:09
问题 Given a ASP.NET MVC application with the following layers: UI (Views, CSS, Javascript, etc.) Controllers Services (Contains business logic, and data access) The reason for no separate data access layer, is that I'm using SQL type provider. (The following code may not be working, as it's only a raw draft). Now imagine a service named UserService defined like: module UserService = let getAll memoize f = memoize(fun _ -> f) let tryGetByID id f memoize = memoize(fun _ -> f id) let add evict f

De Morgan's Laws in Haskell via the Curry-Howard Correspondence

此生再无相见时 提交于 2020-01-01 19:15:54
问题 I implemented three of the four De Morgan's Laws in Haskell: notAandNotB :: (a -> c, b -> c) -> Either a b -> c notAandNotB (f, g) (Left x) = f x notAandNotB (f, g) (Right y) = g y notAorB :: (Either a b -> c) -> (a -> c, b -> c) notAorB f = (f . Left, f . Right) notAorNotB :: Either (a -> c) (b -> c) -> (a, b) -> c notAorNotB (Left f) (x, y) = f x notAorNotB (Right g) (x, y) = g y However, I don't suppose that it's possible to implement the last law (which has two inhabitants): notAandBLeft

How to reduce iterations when chaining map reduce filter?

无人久伴 提交于 2020-01-01 18:37:07
问题 I have been reading about map , reduce and filter a lot because of how much they are used in react and FP in general. If we write something like: let myArr = [1,2,3,4,5,6,7,8,9] let sumOfDoubleOfOddNumbers = myArr.filter(num => num % 2) .map(num => num * 2) .reduce((acc, currVal) => acc + currVal, 0); 3 different loops are run. I've read about Java 8 streams as well and know that they use what is called a monad, ie, the computations are stored first. They are performed once only in one

Functional way of re-using variables in a pipe

自闭症网瘾萝莉.ら 提交于 2020-01-01 15:38:47
问题 Using functional programming in javascript and typescript together with Ramda, I often find myself writing code like: const myFun = c => { const myId = c.id const value = pipe( getAnotherOtherPropOfC, transformToArray, mapToSomething, filterSomething, // ... N other transformations // ok now I need myId and also the result of the previous function chainMyIdWithResultOfPreviousFunction(myId) )(c) return value } Notice how creating a const myId breaks point-free style. I'd like to write myFun

How would you approach this problem in F#? (high frequency sensor data)

非 Y 不嫁゛ 提交于 2020-01-01 12:02:34
问题 I'm a mechanical engineering grad student and my adviser has just asked me to write a data visualization utility for one of our sensor projects. As it's summer and he wants me to have some fun with it, I thought this would be a great time to learn a language adept at scientific computing, so I went ahead and plowed right into F#. As I'm new to the functional programming paradigm I'm having a little difficulty structuring my program properly, especially given the possibility of easily

Functional programming construct for composing identity and side effect

徘徊边缘 提交于 2020-01-01 10:49:27
问题 Does functional programming have a standard construct for this logic? const passAround = (f) => (x) => { f(x); return x; }; This enables me to compose functions that have side effects and no return values, like console.log . It's not like a Task because I don't want to represent the state of the side effect. 回答1: The SKI combinator calculus might interest you. Let's pretend that f is always a pure function: const S = g => f => x => g(x)(f(x)); // S combinator of SKI combinator calculus const