Skip to main content
46 votes

What are the functional equivalents of imperative break statements and other loop checks?

The closest equivalent to looping over an array in most functional languages is a fold function, i.e. a function that calls a user-specified function for each value of the array, passing an ...
Jules's user avatar
  • 17.8k
33 votes

What are the functional equivalents of imperative break statements and other loop checks?

You could easily convert it to recursion. And it has nice tail-optimized recursive call. Pseudocode : public int doSomeCalc(int[] array) { return doSomeCalcInner(array, 0); } public int ...
Euphoric's user avatar
  • 37.5k
14 votes

What are the functional equivalents of imperative break statements and other loop checks?

I really like Jules' answer, but I wanted to additionally point out something people often miss about lazy functional programming, which is that everything doesn't have to be "inside the loop." For ...
Karl Bielefeldt's user avatar
8 votes

How Functional Programming addresses concurrent increment/decrement operations invoked by different users?

You are asking how functional languages handles shared mutable state. Functional languages do prefer and encourage immutable data structures, but they all (AFAIK) have provisions for mutable shared ...
JacquesB's user avatar
  • 59.9k
7 votes

What practical problem results from lack of hygienic macros in Clojure?

The advantage of hygienic macros is not one of language capability -- you can write macros that have good hygiene using gensym and careful quoting/unquoting at the right times. However, hygienic ...
Nathan Davis's user avatar
6 votes

What are the functional equivalents of imperative break statements and other loop checks?

Most list processing examples you will see use functions like map, filter, sum etc. which operate on the list as a whole. But in your case you have a conditional early exit - a rather uncommon pattern ...
JacquesB's user avatar
  • 59.9k
5 votes

What are the functional equivalents of imperative break statements and other loop checks?

As pointed out by other answers, Clojure has reduced for stopping reductions early: (defn some-calc [coll] (reduce (fn [answer e] (let [answer (+ answer e)] (case answer ...
nilern's user avatar
  • 59
4 votes

How do people get rid of conditional branches in Functional Programming?

This is a very old question, but I feel like the answers were lacking. As you mentioned, in OO branching is frequently moved to the class level. Let's think about what that means: Factory method ...
Daniel's user avatar
  • 141
4 votes

Can functional programming used for solving problems which require randomness?

Your question in a bit unclear in that you don't specify whether you are talking about true random numbers or pseudo-random numbers, so I will answer both. True Random Numbers You are correct that ...
Jörg W Mittag's user avatar
3 votes

Can functional programming used for solving problems which require randomness?

The whole point of a random number generator is to return a value that is not a deterministic function of its input. A "random number function" is therefore a contradiction in terms. It is not even ...
Steve's user avatar
  • 9,272
3 votes

If you can use def to redefine variables how is that considered immutable?

Clojure is all about immutable data Clojure is about managing mutable state by controlling the mutation points (i.e., Refs, Atoms, Agents, and Vars). Though, of course, any Java code you use via ...
Nathan Davis's user avatar
2 votes

How to represent hard-to-calculate "properties" of "objects" in functional code?

First of all, make sure you really have a problem. Profiling and benchmarking are key. There are a multitude of options, and ultimately it's up to you to determine which one(s) is most suited for ...
Nathan Davis's user avatar
2 votes

Should I provide partialed functions based on API call in Clojure?

OO requires objects have mutable attributes No, OO requires objects have attributes. There is a lot of OO code that uses objects with mutable attributes, and also a lot of OO code that uses objects ...
Caleth's user avatar
  • 11.5k
2 votes

Python decorators and Lisp macros

A decorator (be it in Python or in any other - functional programming - language) is just a function which accepts an original (to-be-decorated) function (and sometimes more additional arguments), and ...
Gwang-Jin Kim's user avatar
2 votes

Does Clojure have continuations/coroutines/etc?

While Clojure does not have first-class continuations or coroutines built-in as a core feature, it is possible to implement your own. For example, core.async is a Clojure library that implements the ...
Nathan Davis's user avatar
2 votes

What are the functional equivalents of imperative break statements and other loop checks?

The intricate part is the loop. Let us start with that. A loop is usually converted to functional style by expressing the iteration with a single function. An iteration is a transformation of the loop ...
libeako's user avatar
  • 159
2 votes

How can we handle inserts/delete cases using Map like data structures in FP?

While functional programming emphasises pure functions, a pure function alone can't define a useful software system. You need side effects to observe its behaviour, and that's impure. In practical ...
Mark Seemann's user avatar
  • 3,920
2 votes
Accepted

In clojure, how to structure a simple reverse file search function to make it unit testable

I am not fluent in Clojure, but why not pass the function for finding the parent path as a parameter to find-config-path, where .getParentFile is used as default value? Something along the lines of (...
Doc Brown's user avatar
  • 210k
1 vote
Accepted

Is Clojure a wrong choice for implementing Data Structures?

Traditional data structures are mutable by nature. You manipulate them by changing pointers/references and modifying their contents. That's hard to do in an immutable language; you essentially have ...
Robert Harvey's user avatar
1 vote

What are the functional equivalents of imperative break statements and other loop checks?

First, rewrite the loop slightly, such that each iteration of the loop either early-exits, or mutates answer exactly once: public int doSomeCalc(int[] array) { int answer = 0; ...
Joseph Sible-Reinstate Monica's user avatar
1 vote

Can functional programming used for solving problems which require randomness?

Short Answer Randomness is handled just like other kinds of input. Long Answer Random is Input Random is like reading the state of the hardware clock. And that is like reading the state of the ...
libeako's user avatar
  • 159
1 vote
Accepted

Efficient algorithm for deducing object type dynamically based on members

You might have a look at a RETE algorithm. Let's use a working definition of rule as something that pairs a match expression with an action expression, and where success of the former triggers the ...
Erik Eidt's user avatar
  • 34.2k
1 vote

Why does Clojure neglect the uniform access principle?

I'll add one more answer related to the comment: To clarify what I meant saying UAP: assume in Ruby in class User I have an attribute name. So I access it like user_instance.name. But then I can ...
Didier A.'s user avatar
  • 1,387
1 vote

What did Rich Hickey mean when he said, "All that specificity [of interfaces/classes/types] kills your reuse!"

I know this is an old question, but I feel like I have to provide an answer because even the accepted one is not what Rich Hickey meant. What he is talking about is broader than types. For example, a ...
krzyzowiec's user avatar
1 vote

Setting up Clojure Project And Sub Projects

Have you thought of adopting a mono-repo approach? Even scoped at the project-level, rather than necessarily company-wide. In which case, consider using lein-sub and/or lein-cascade Take a look at ...
codeasone's user avatar
  • 111
1 vote

Does Clojure have continuations/coroutines/etc?

Does Clojure have continuations or coroutines to do tasks like ping-ponging without a stack overflow? Old question, so I am not even sure whether this feature was available at the time, but for ...
alex gian's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible