Tagged Questions
1
vote
2answers
175 views
How to go about composing core functions, rather then using imperative style?
I have translated this code, the snippet below, from Python to Clojure. I replaced Python's while construct with Clojure's loop-recur here. But this doesn't look idiomatic.
(loop [d 2 [n & more] ...
2
votes
2answers
43 views
In clojure, how to make a search procedure return as soon as the answer is found
Clojure newbie here.
I was solving a searching problem in Clojure.
In search problem, it's quite common to return early as soon as the answer is found.
For example, in Java
boolean search(State x) ...
1
vote
2answers
88 views
Building a map from a vector
So I have a vector which sort of looks like this
["John" 23 "5551234" "Sally" 34 "5556667"]
the vector contains a lot more entries like this, what I am trying to do is make a vector of maps like ...
3
votes
5answers
162 views
Is immutability in clojure different than pass-by-value?
I'm just getting started with Clojure and I have no fp experience but the first thing that I've noticed is a heavy emphasis on immutability. I'm a bit confused by the emphasis, however. It looks ...
0
votes
1answer
20 views
- Clojure - I get an error when I run this fibonacci function, any idea what's wrong?
I picked up clojure a few hours ago and am trying to get a Fibonacci function working
Here is the function I'm defining
(defn fib [x]
(if (or (= x 1) (= x 2))
((inc 0))
((+ (fib (- x 1)) ...
0
votes
4answers
132 views
How do convert this code and my thinking to a functional mindset (Clojure)?
How do I convert this JavaScript code to Clojure?
I am trying to draw a (x,y) world where the cells are on or off according to the fill property. In the example below I am trying to print the rows ...
1
vote
1answer
50 views
Comparison based on Clojure types
I have the following record type that I am trying to test:
(defrecord FirstOrderState [datum matrix]
State
;; implementation goes here ...
)
I am trying to branch based on the above type, but ...
2
votes
4answers
113 views
Divisor function in clojure
I'm new to clojure and I want to create a function which returns a vector of all divisors of a certain Number.
For instance:
[1 2 3] for 6 as input
(defn div [x seq]
(let [r (range 1 (+ (/ x 2) ...
0
votes
1answer
56 views
Fail to define a Class in Clojure?
I was following the example on Clojure in Action Page 326,
(defn new-object [klass]
(fn [command & args]
(condp = command
:class klass)))
Then I typed: (def cindy (new-object ...
0
votes
2answers
122 views
Clojure concurrency : Automating SQL Queries
I have a small program that is supposed to read SQL queries/commands one by one and execute them against a database.
If a query executes successfully, the next query is executed.
If there is an ...
6
votes
1answer
309 views
What functional languages have good support for iOS and Android development?
Given that Android is JVM-based I’m guessing JVM languages like Clojure and Scala would work out-of-the-box. Is that correct? Are there any caveats?
I thought OCaml might have a great Android story ...
4
votes
3answers
158 views
How to properly use “iterate” and “partial” in Clojure?
Most reference to iterate are for operators, and all the applications on functions are so confusing that I still don't get how to use iterate in my code, and what partial is.
I am doing a programming ...
2
votes
1answer
114 views
How to cast a character to int in Clojure?
How to cast a character to int in Clojure?
I am trying to write a rot 13 in clojure, so I need to have something to cast my char to int. I found something called (int), so I put:
(int a)
Get: ...
0
votes
1answer
126 views
Modelling game UI screens in Clojure
I love Lisp in an abstract kind of way. I have used Scheme before and it is great for programs that can be modelled as one big pure function.
I am trying to write a game in Clojure using Quil.
...
1
vote
3answers
163 views
Pass multiple parameters function from other function with Clojure and readability issues
I'm trying to learn functional programming with SICP. I want to use Clojure.
Clojure is a dialect of Lisp but I'm very unfamiliar with Lisp. This code snippet unclean and unreadable. How to write ...
5
votes
2answers
223 views
How to test higher order functions in Clojure?
How do I test a higher-order function in Clojure ?
I can always test a function that accepts a value and then check returned value against the expected one.
How do I do that with a higher order ...
2
votes
3answers
121 views
Converting an imperative algorithm into functional style
I wrote a simple procedure to calculate the average of the test coverage of some specific packages in a Java project. The raw data in a huge html file is like this:
<body>
package pkg1 ...
3
votes
1answer
137 views
Read a file in clojure and ignore the first line?
Using code from this answer, I have
(defn repeat-image [n string]
(println (apply str (repeat n string))))
(defn tile-image-across [x filename]
(with-open [rdr (reader filename)]
(doseq ...
4
votes
1answer
93 views
Using 'map' with different sized collections in clojure
I'd like to understand the idiomatic way with which to operate over collections of different sizes in clojure. Is there a way I can tell the function 'map' to pad the rest of a collection with some ...
0
votes
1answer
56 views
Efficient view updating with functional data model
In functional programming, data models are immutable, and updating a data model is done by applying a function on the data model, and getting a new version of the data model in return. I'm wondering ...
2
votes
1answer
92 views
Functional way to make multiple API requests with Clojure
I'm working on a Clojure application that will interact with a web API to return a random result meeting a specific criterion. Because of the limitations of the API, I have to retrieve a number of ...
1
vote
4answers
377 views
Cartesian product in clojure
I'm trying to implement a method that will take a list of lists and return a the cartesian product of these lists.
Here's what I have so far:
(defn cart
([] '())
([l1] (map list l1))
([l1 l2]
...
4
votes
4answers
196 views
thinking objectively vs. functionally [closed]
As I try to increase my knowledge of functional programming, I'm finding it quite difficult to re-imagine the solutions to problems I've solved in an OOP language in terms of functions, particularly ...
1
vote
1answer
112 views
Pure functional tree with parent pointer
I know that RB tree with left and right child can be implemented in pure functional way without degrading log n performance. Can tree with parent pointer be implemented in logarithm time? Seems like ...
0
votes
4answers
68 views
Getting index and size of collection when using the map function
I have this scenario. I use the map function in Clojure to apply a custom function on a collection. This function takes a while to execute and my collection is pretty large. I would like to be able to ...
2
votes
3answers
103 views
What does this one line of Clojure code do?
(map #(words %) indexes)
words is a vector of strings and indexes is a sequence of non-negative integers. I understand that #(...) is an anonymous function and % represents the arguments to it. I ...
4
votes
3answers
124 views
In FP how do you set a reciprocal relationship?
In FP where there is no mutable state and every operation returns a new state of the world. Given: I have a contact list and an individual contact.
I add Dirk to my address book. Dirk is a child of ...
2
votes
1answer
90 views
Is there a better way to do that in Clojure?
I have this function to read a file and convert it to a list of two-elements lists:
(def f1 "/usr/example")
(defn read-file [file]
(let [f
(with-open [rdr (clojure.java.io/reader file)]
...
3
votes
1answer
131 views
Fibonacci numbers in Clojure
I'm learning Clojure. Quite basic task is to generate Fibonacci sequence. I end up with pretty much copy of the imperative solution (and list is reversed, huh):
(defn n-fib [n]
(if (= n 1) '(1)
...
3
votes
2answers
170 views
clojure frequency dictionary from big data
I want to write my own naive bayes classifier
I have a file like this:
(This is database of spam and ham messages, first word points to spam or ham, text until eoln is message (size: 0.5 Mb) from ...
5
votes
8answers
1k views
How loops implemented in Functional languages ?
It is said that Functional Languages don't have loops.
So please explain how this languages implement this functionality.
For example, I cannot understand how a Map function iterates over each ...
4
votes
3answers
345 views
Better Function Composition in Python
I work in Python. Recently, I discovered a wonderful little package called fn. I've been using it for function composition.
For example, instead of:
baz(bar(foo(x))))
with fn, you can write:
...
3
votes
2answers
71 views
How can I recur from a Clojure catch block in a more functional manner?
For an IRC bot I'm writing, I want to keep trying to open a socket, even if there's an UnkownHostException. My first thought was something like this.
(defn open-socket [{:keys [host port] :as ...
3
votes
1answer
95 views
walk through tree functionally
I'm try to writemy own "walk through tree" function, but I'm new in FP
I've wrote these function and it works nice, but it looks ugly
(defrecord Tree [value left right])
(defn depth_walk_tree
...
4
votes
5answers
153 views
Clojure functions - returning value computed before the last statement
I have some tests written in clojure. This is a simple example:
(defn test1
[]
(start-server)
(run-pvt-and-expect "PVT-0")
(stop-server)
)
I would like to return the result of ...
1
vote
1answer
78 views
clojure sum of all the primes under 2000000
It's a Project Euler problem .
I learned from Fastest way to list all primes below N in python
and implemented a clojure :
(defn get-primes [n]
(loop [numbers (set (range 2 n))
primes []]
...
18
votes
2answers
869 views
How to compare two functions for equivalence, as in (λx.2*x) == (λx.x+x)?
Is there a way to compare two functions for equality? For example, (λx.2*x) == (λx.x+x) should return true, because those are obviously equivalent.
15
votes
5answers
991 views
What is a solid example of something that can be done with list comprehensions that is tricky with high order functions?
I've heard from many Pythonists that they prefer list comprehensions because they can do everything you can do using high order functions such as filter and reduce, and more. So this question address ...
1
vote
1answer
73 views
Call function for all possible combinations of vectors
I have been using Clojure for the past week since I continued a project of a colleague of mine. We are using Clojure to generate some files. I am trying to refactor some code since he had to do it ...
1
vote
0answers
186 views
Can every recursive function be rewritten using tail calls? [duplicate]
Can every recursive function be rewritten using tail calls? If not, what are examples of recursive functions for which this can't be done?
3
votes
1answer
84 views
Reducing a sequence into a shorter sequence while calling a function on each adjacent element
I've got a function that looks at two of these objects, does some mystery logic, and returns either one of them, or both (as a sequence).
I've got a sequence of these objects [o1 o2 o3 o4 ...], and ...
9
votes
3answers
313 views
How to write idiomatic clojure (+ functional) code?
I just started hacking around with Clojure, and although I adore the language, I can't understand how to do certain things idiomatically.
Writing a web-app using compojure, here's one of my ...
3
votes
1answer
124 views
Functional Programming: persistent list that handles freq. item updates efficiently?
Given:
You have a list of items.
You’re using persistent data structures.
You will make frequent updates to the persistent items (data structures) in your list.
An item being modified is likely to ...
3
votes
3answers
97 views
Idiomatic way to use for, while still maintaining high performance
I have a map that is sorted by its keys which contains data like this:
(def h {50 Text1
70 Text2
372 Text1
391 Text2
759 Text1
778 Text2
})
The map is sorted by Keys. ...
2
votes
3answers
203 views
Clojure: What is wrong with my implementation of flatten?
I've been working through problems on 4clojure today, and I ran into trouble on #28, implementing flatten.
There are a couple of definite problems with my code.
(fn [coll]
((fn flt [coll res]
...
1
vote
2answers
94 views
Which term is used to describe functions with multiple “modes”?
Clojure's range function, for example, has four modes:
Usage: (range)
(range end)
(range start end)
(range start end step)
Returns a lazy seq of nums from start (inclusive) to ...
2
votes
3answers
145 views
What is the closest match to this Clojure map/apply expression, in Rebol?
While comparing functional expressions in Clojure side-by-side with Rebol, I happened onto this expression from the examples of apply used in combination with map, at clojure-docs.org:
user=> (map ...
1
vote
2answers
288 views
Clojure way of reading large files and transforming data therein
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 ...
9
votes
1answer
333 views
Clojure head retention
I'm reading Clojure Programming book by O'Reilly..
I came over an example of head retention.
First example retains reference to d (I presume), so it doesnt get garbage collected:
(let [[t d] ...
2
votes
1answer
96 views
next and rest in clojure
I am reading explanations on rest vs next in clojure.
As I understand it, it breaks down to next evaluating/realizing the tail of the sequence,
to know wether it should return nil or not, while rest ...