Functional programming is a paradigm which attempts to solve computational problems by the chained evaluation of functions whose output is determined by their inputs rather than the programme state. In this style of programming, side effects and mutable data are deprecated and usually strictly ...

learn more… | top users | synonyms (1)

0
votes
0answers
10 views

How to deal a card using F# [migrated]

I've been working on modeling a popular card game (Love Letter) using F# to learn more about functional programming. module Game = open Cards open Players type Deck = Card list let dealACard ...
6
votes
3answers
191 views

Is there a Haskell idiom for trying several functions and stop as soon as one succeeds?

In Haskell, I can use the type a -> Maybe b to model a function that either returns a value of type b, or returns nothing (it fails). If I have types a1, ..., a(n+1) and functions f1, ..., fn, ...
1
vote
1answer
62 views

The difference between bind and _.curry

So JavaScript's bind supports currying, but most people use some other library like lodash or ramda to do currying. From first impression It seems like bind supports context changing, since that is ...
5
votes
2answers
93 views

What is the difference between currying and partial function application in practice

I understand the difference between partial function application and a curried function (f(X x Y x Z) -> N vs f(X -> (Y -> (Z -> N)))), but I do not see what the consequence of this ...
30
votes
2answers
1k views

What is the meaning of “doesn't compose”?

I see a lot of texts, especially functional programming texts, claim that certain CS concepts "don't compose". Examples are: locks don't compose, monads don't compose. I've been having a hard time ...
2
votes
2answers
90 views

Is the function using python list comprehension, stateless?

Below is the function count_leaf, that appends mutable list branch_counts, which is not stateless. def count_leaf(tree): if is_leaf(tree): return 1 branch_counts = list() for b in ...
0
votes
0answers
56 views

How to ensure this constraint at compile time?

I have an interesting constraint/invariant and I would love to hear people's input on how to maintain it. Big bonus if it can be ensured at compile time... I have the types below, and the following ...
1
vote
2answers
125 views

What is the advantage of global functions when writing functional code

I am a Swift developer and am trying to adopt a functional / reactive style in my code. I have been using ReactiveCocoa in all my projects and I have started giving RAC 3.0 a try. One thing I have ...
1
vote
3answers
98 views

Using delegates to avoid duplicate creation of resources

I'm writing a PCL that uses an HttpClient to go visit a few sites and extract data from them. My initial code looked like this: public static class Download { public async static ...
2
votes
0answers
54 views

How to handle complex calculated fields in an ORM

In our API we've got a few central datatypes which need to be "decorated" (so to speak) after retrieval from the database with calculated values. The database is accessed through an ORM which follows ...
2
votes
2answers
187 views

How do I make this functional DSL written in an imperative language more efficient?

Suppose I create a simple functional Domain-specific language (DSL) using an imperative language, in this case C++. Here is a simple implementation of a DSL that can has the notion of a simple value ...
15
votes
3answers
2k views

What problem do algebraic data types solve?

Fair warning, I'm new to functional programming so I may hold many bad assumptions. I've been learning about algebraic types. Many functional languages seem to have them, and they are fairly useful ...
14
votes
5answers
927 views

Is the semantic contract of an interface (OOP) more informative than a function signature (FP)?

It is said by some that if you take SOLID principles to their extremes, you end up at functional programming. I agree with this article but I think that some semantics are lost in the transition from ...
30
votes
6answers
6k views

Why do programs use call stacks, if nested function calls can be inlined?

Why not have the compiler take a program like this: function a(b) { return b^2 }; function c(b) { return a(b) + 5 }; and convert it into a program like this: function c(b) { return b^2 + 5 }; ...
4
votes
2answers
129 views

Are (basic) SQL queries semantically equivalent to Higher Order Functions?

Is SQL basically a domain specific instance of map + fold + filter? It seems to me that the following SQL: SELECT name FROM fruits WHERE calories < 100 is just syntactic sugar for the ...
8
votes
1answer
179 views

Is it possible to have currying and variadic function at the same time?

I am thinking about making currying and variadic functions both available in a dynamically-typed functional programming language, but I wonder if it is possible or not. Here are some pseudocode: sum ...
32
votes
9answers
4k views

Why would a program use a closure?

After reading many posts explaining closures here I'm still missing a key concept: Why write a closure? What specific task would a programmer be performing that might be best served by a closure? ...
1
vote
2answers
104 views

distinguish requests from the same vehicle

I am implementing an small app to track buses based on Crowdsorcing. The riders send data long, lat, mac, route to the server as JSON string. In my database I have table bus to insert the transmitted ...
1
vote
1answer
88 views

Are There Tools for Statically Evaluating C++ Functions for Side Effects? [closed]

I'm interested in having a tool that can evaluate whether a function in C++ has side effects. I'd like to be able to mark functions as having or not having side effects, which I think would make ...
1
vote
1answer
207 views

Why can a constructor be used without `new` keyword in Javascript?

I found Date can be used without the new keyword. Date(1) > "Thu May 28 2015 15:54:20 GMT+0800 (CST)" new Date(1) > Thu Jan 01 1970 08:00:00 GMT+0800 (CST) I was wondering whether there is ...
4
votes
2answers
125 views

Functional Programming - Functions defining specific evaluation of functions passed to it for optimization

Firstmost, I am just getting started with functional programming so I would appreciate corrections in any terminology I may have used incorrectly. Story time, While doing a Project Euler Problem 1 in ...
5
votes
2answers
176 views

How to design for good abstractions using algebraic data type?

Every now and then I have peaked at Haskell Tutorials and found the Algebraic data types quite interesting. I took their purpose to be to represent types that have completely separable states. Sadly, ...
6
votes
3answers
175 views

What is the functional programming answer to type-based invariants?

I am aware that the concept of invariants exists across multiple programming paradigms. For example, loop invariants are relevant in OO, functional and procedural programming. However, one very ...
7
votes
4answers
362 views

What makes functional programming languages declarative as opposed to Imperative?

On many articles, describing the benefits of functional programming, I have seen functional programming languages, such as Haskell, ML, Scala or Clojure, referred to as "declarative languages" ...
3
votes
3answers
370 views

Functional vs object-oriented style in C#

I'm learning functional programming and face the following confusion when applying it to my C# projects: I begin by writing pure, static functions and use function composition. After the code ...
7
votes
3answers
251 views

Functional programming and Text adventures

This is mostly a theoretical question about FP, but I'll take text adventures (like old-school Zork) to illustrate my point. I'd like to know your opinions on how would you model a stateful simulation ...
2
votes
1answer
271 views

Should all functions be fully self-contained (is it bad practice to share a variable between functions)?

There are two ways to do the same thing (pseudo code) Define databaseHandle in the parent function, and use it as a global in this scope: function API() { function openDatabase() { ...
1
vote
1answer
288 views

What aspects of Haskell led to its rise in popularity among experts?

20 years ago, the Functional Programming world was all about Lisp and Scheme. When I went to college in 2001, my Fall Semester Freshman CS 101 course was taught in OCaml. However, these days the ...
0
votes
1answer
120 views

Haskell types for functions

I don't understand the answer to this question: Q: Can Haskell find a type for the function selfapply defined by: selfapply f = f f A: The function selfapply is not typeable in the simple system of ...
1
vote
1answer
266 views

What is the “->” symbol called?

I have seen the -> operator/symbol in Java 8 predicates recently and wondered what its name is. I know that it is used in lambda expressions, but I know that the symbol for lambda is λ, so that's ...
2
votes
2answers
105 views

Dealing with states in a immutable approach [closed]

I want to know how to deal when you have some states in a program, with functions that depends on them, but with a immutable approach. I read some examples and questions, but all focus in a small ...
0
votes
3answers
150 views

Why python function programming functions are not collection methods? [duplicate]

In other words, is there a Python design related reason for it to be so? Functions like map, filter, reduce etc. are just plain functions. Is it just a poor design choice (if it is a good one, ...
9
votes
2answers
378 views

Maintaining State without assignment

I am learning functional programming and I have trouble understanding how some particular scenarios are implemented without the use of assignment. The following simple problem pretty much sums up my ...
1
vote
1answer
224 views

Advantages of the imperative style over the functional style [duplicate]

There's a lot of hype over functional languages right now, and I've spent the last year studying Haskell as my intro to FP as a result. Seeing the advantages FP provides is easy (such as referential ...
4
votes
1answer
147 views

Pattern matching against two similar types

What is the best way to handle pattern matching in the following situation? sealed trait Metadata final case class Metadata1() extends Metadata final case class Metadata2() extends Metadata final ...
0
votes
0answers
33 views

Accessing intermittently available resources with transactions and post-access cleanup in a generalized, functional, composable way

The following psuedo-code illustrates what I'd like to be able to do. Is there a way to accomplish something like this in Scala?: trait IntermittentlyAvailableResource trait ...
0
votes
1answer
69 views

FP: Capturing the characteristics of a process which blocks, causes side-effects, and may fail

I have a driver function modifyFile that interacts with many sources in the outside world (e.g. HTTP, filesystem). Let's say the code is as such: def downloadFile(from: String, to: String): Try[Unit] ...
12
votes
2answers
346 views

Is higher-rank parametric polymorphism useful?

I'm pretty sure everyone is familiar with generic methods of the form: T DoSomething<T>(T item) This function is also called parametrically polymorphic (PP), specifically rank-1 PP. Let's ...
11
votes
3answers
3k views

What is the name of a function that takes no argument and returns nothing? [closed]

In Java 8's java.util.function package, we have: Function: Takes one argument, produces one result. Consumer: Takes one argument, produces nothing. Supplier: Takes no argument, produces one result. ...
8
votes
2answers
235 views

Is it common practice to transform requirement specifications into predicate logic for functional programming?

I've recently been assigned to work on a small project which is being implemented in Haskell. Coming from an OO/imperative background, I'm used to converting requirements/user-stories into use-cases ...
2
votes
0answers
174 views

What is the relationship between “flux” and pure functional reactive programming?

Flux, as far as I understood, is a technique about dealing with the dataflow of an application unidirectionally, keeping state isolated from the rest of the program in read-only, self-contained ...
5
votes
3answers
519 views

Is Functional Programming a viable alternative to dependency injection patterns?

I have recently been reading a book entitled Functional Programming in C# and it occurs to me that the immutable and stateless nature of functional programming accomplishes similar outcomes to ...
2
votes
1answer
87 views

FP and board game state

I wanted to implement the game of Tic Tac Toe using a functional language (in my case, Scala) but I'm unsure how to go about managing the board game state after each player makes their move. I ...
1
vote
1answer
141 views

Functional Programming style: How to write functions - explicit currying, implicit currying or lamdas?

So I have been using F# for a while and studying a bit of Haskell on the side and I have realized I could rewrite the exact same function one of three different ways. Either with implicit currying, ...
0
votes
3answers
442 views

Is functional language without runtime written in C possible? [closed]

Every functional language that compiles to native code relies on quite big runtime written in C programming language (well at least ones that I know of, for example Haskell, OCaml, Gambit/Chicken ...
-1
votes
1answer
91 views

Significance of '__name__ ' attribute in python

1) Below python code, >>> def f(): return creates a function type object which has __name__ attribute with value 'f' which looks fine. But, 2) Below line of code, >>> x = ...
6
votes
3answers
816 views

Can “higher order function” feature allow/maintain abstraction and encapsulation?

Below is the function repeat written using a functional paradigm, such that when called as repeat(square, 2)(5) it will apply the square function 2 times on the number 5, something like ...
1
vote
0answers
115 views

How and where to make global function file in jsp sevlet web application

I am making a website in Servlet&JSP. I am looking for good way to make a specific function file from where I can call and declare my common functions. If I made a java file function. Java, so is ...
-4
votes
4answers
140 views

Does this python program obey functional paradigm?

Below is the hailstone sequence program considering the rule of thumb in functional programming. The simple rule of thumb is: if you can replace any expression, sub-expression or subroutine call ...
4
votes
1answer
111 views

How would I isolate changes to mutable state if I need to run two queries to get the final result?

I'm working on some code that takes search criteria from a Rest API and uses it to query a remote API to return results. As an exercise, I wanted to try to separate all state changes to one place, as ...