functional-programming

Scala: collecting updates/changes of immutable state

人盡茶涼 提交于 2019-12-20 17:31:31
问题 I'm currently trying to apply a more functional programming style to a project involving low-level (LWJGL-based) GUI development. Obviously, in such a case it is necessary to carry around a lot of state, which is mutable in the current version. My goal is to eventually have a completely immutable state, in order to avoid state changes as side effect. I studied scalaz's lenses and state monads for awhile, but my main concern remains: All these techniques rely on copy-on-write. Since my state

time-based simulation with actors model

我是研究僧i 提交于 2019-12-20 14:42:56
问题 we have a single threaded application that simulates the interaction of a hundred of thousands of objects over time with the shared memory model. obviously, it suffers from its inability to scale over multi CPU hardware. after reading a little about agent based modeling and functional programming/actor model I was considering a rewrite with the message-passing paradigm. the idea is very simple - each object will be an actor and their interactions will be messages so that the simulation could

How to perform side-effects in pure functional programming?

喜你入骨 提交于 2019-12-20 12:34:35
问题 I am dealing with the concept of functional programming for a while now and find it quite interesting, fascinating and exciting. Especially the idea of pure functions is awesome, in various terms. But there is one thing I do not get: How to deal with side-effects when restricting yourself to pure functions. E.g., if I want to calculate the sum of two numbers, I can write a pure function (in JavaScript): var add = function (first, second) { return first + second; }; No problem at all. But what

Idiomatic way to write Clojure code for repeatedly reading lines from the console?

徘徊边缘 提交于 2019-12-20 12:34:27
问题 Recently I was writing a little CLI script which needed to repeatedly read dates from the console (the number of dates to read was calculated and could be different each time). Sample Ruby code to give you the idea: dates = x.times.collect { print "Enter date: "; Date.parse(gets.chomp) } Just for the heck of it, I wrote the script in Clojure, and wound up using some rather ugly code with swap! and loop...recur . I'm wondering what the cleanest way to achieve the desired effect in Clojure

How to not fall into R's 'lazy evaluation trap'

情到浓时终转凉″ 提交于 2019-12-20 12:28:41
问题 "R passes promises, not values. The promise is forced when it is first evaluated, not when it is passed.", see this answer by G. Grothendieck. Also see this question referring to Hadley's book. In simple examples such as > funs <- lapply(1:10, function(i) function() print(i)) > funs[[1]]() [1] 10 > funs[[2]]() [1] 10 it is possible to take such unintuitive behaviour into account. However, I find myself frequently falling into this trap during daily development. I follow a rather functional

Mapping a function on a generator in JavaScript

十年热恋 提交于 2019-12-20 12:24:59
问题 I have a generator called generateNumbers in JavaScript and another generator generateLargerNumbers which takes each value generated by generateNumbers and applies a function addOne to it, as such: function addOne(value) { return value + 1 } function* generateNumbers() { yield 1 yield 2 yield 3 } function* generateLargerNumbers() { for (const number of generateNumbers()) { yield addOne(number) } } Is there any terser way to do this without building an array out of the generated values? I'm

Functional/Immutable Data Structures for the JVM? [closed]

痞子三分冷 提交于 2019-12-20 11:54:07
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 months ago . Does anyone know of a Java/JVM data structure library providing functional (a.k.a. immutable, or "persistent" in the functional sense) equivalents of the familiar Java data structures? By "functional" I mean that the objects themselves are immutable, while modifications to those objects return new objects

Embedding higher kinded types (monads!) into the untyped lambda calculus

狂风中的少年 提交于 2019-12-20 11:09:03
问题 It's possible to encode various types in the untyped lambda calculus through higher order functions. Examples: zero = λfx. x one = λfx. fx two = λfx. f(fx) three = λfx. f(f(fx)) etc true = λtf. t false = λtf. f tuple = λxyb. b x y null = λp. p (λxy. false) I was wondering if any research has gone into embedding other less conventional types. It would be brilliant if there is some theorem which asserts that any type can be embedded. Maybe there are restrictions, for example only types of kind

ngrx dealing with nested array in object

别说谁变了你拦得住时间么 提交于 2019-12-20 10:37:09
问题 I am learning the redux pattern and using ngrx with angular 2. I am creating a sample blog site which has following shape. export interface BlogContent { id: string; header: string; tags: string[]; title: string; actualContent: ActualContent[]; } and my reducer and actions are as following: import { ActionReducer, Action } from '@ngrx/store'; import * as _ from 'lodash'; export interface ActualContent { id: string; type: string; data: string; } export interface BlogContent { id: string;

scala game programming: advancing object position in a functional style

落爺英雄遲暮 提交于 2019-12-20 10:35:17
问题 Long time java programmer slowly learning scala (loving it by the way), and I think my mind is still wrapping itself around the concept of writing things functionally. Right now I'm attempting to write a simple visualizer for some moving 2d textures. The imperative approach is simple enough, and I'm sure most of you will recognize this relatively ubiquitous block of code (stuff was changed to protect the innocent): class MovingTexture(var position, var velocity) extends Renders with Advances