Showing posts with label Clojure. Show all posts
Showing posts with label Clojure. Show all posts

Thursday, December 2, 2021

How to set Java version for lein

To select specific Java (JDK) to use for leiningen, set environment variable JAVA_CMD

For example,

JAVA_CMD=/path/to/bin/java lein repl

Credit: https://gist.github.com/camsaul/c982019fd915510677236cd4b720a583#gistcomment-3439527 

Saturday, December 9, 2017

Clojure has lesser typing error problems than the others well-known dynamic typing languages

Give these snippets a try.
// JS
function foo () {
  bar(1)
}
# Ruby
def foo
  bar(1)
end
; Clojure
(defn foo [] 
  (bar 1))

As you can see from the images below, Clojure doesn't allow you to refer to an undeclared symbol. I believe this takes away quite a bit of typing error issues.

Tuesday, August 23, 2016

[Solved] An obscure error in case somebody else gets it too

I fixed an error below with `lein clean`. Hope this post can save your time as it used mine.
#error {
  :cause clojure.core$seq_QMARK___5848
    :via
    [{:type clojure.lang.Compiler$CompilerException
      :message java.lang.NoClassDefFoundError: clojure/core$seq_QMARK___5848, compiling:(clj_gatling/report.clj:1:1)
        :at [clojure.lang.Compiler load Compiler.java 7415]}
  {:type java.lang.NoClassDefFoundError
    :message clojure/core$seq_QMARK___5848
      :at [clojure.core.cache$lru_cache_factory invokeStatic cache.clj 581]}
    {:type java.lang.ClassNotFoundException
      :message clojure.core$seq_QMARK___5848
        :at [java.net.URLClassLoader findClass URLClassLoader.java 381]}]
        :trace
        [[java.net.URLClassLoader findClass URLClassLoader.java 381]
        [java.lang.ClassLoader loadClass ClassLoader.java 424]
        [sun.misc.Launcher$AppClassLoader loadClass Launcher.java 331]
        [java.lang.ClassLoader loadClass ClassLoader.java 357]
        [clojure.core.cache$lru_cache_factory invokeStatic cache.clj 581]
        [clojure.core.memoize$lru$fn__2765 invoke memoize.clj 334]
        [clojure.lang.AFn applyToHelper AFn.java 160]
        [clojure.lang.AFn applyTo AFn.java 144]
        [clojure.core$apply invokeStatic core.clj 659]
        [clojure.core.memoize$build_memoizer invokeStatic memoize.clj 152]
        [clojure.core.memoize$lru invokeStatic memoize.clj 332]
        [clojure.core.memoize$lru invoke memoize.clj 292]
        [clojure.core.memoize$lru invokeStatic memoize.clj 328]
        [clojure.core.memoize$lru invoke memoize.clj 292]
        [clojure.tools.analyzer.jvm.utils__init load nil 79]
        [clojure.tools.analyzer.jvm.utils__init nil -1]
        [java.lang.Class forName0 Class.java -2]
        [java.lang.Class forName Class.java 348]
        [clojure.lang.RT classForName RT.java 2183]
        [clojure.lang.RT classForName RT.java 2192]
        [clojure.lang.RT loadClassForName RT.java 2211]
        [clojure.lang.RT load RT.java 445]
        [clojure.lang.RT load RT.java 421]
        [clojure.core$load$fn__7645 invoke core.clj 6008]
        [clojure.core$load invokeStatic core.clj 6007]
        [clojure.core$load doInvoke core.clj 5991]
        [clojure.lang.RestFn invoke RestFn.java 408]
        [clojure.core$load_one invokeStatic core.clj 5812]
        [clojure.core$load_one invoke core.clj 5807]
        [clojure.core$load_lib$fn__7590 invoke core.clj 5852]
        [clojure.core$load_lib invokeStatic core.clj 5851]
        [clojure.core$load_lib doInvoke core.clj 5832]
        [clojure.lang.RestFn applyTo RestFn.java 142]
        [clojure.core$apply invokeStatic core.clj 659]
        [clojure.core$load_libs invokeStatic core.clj 5889]
        [clojure.core$load_libs doInvoke core.clj 5873]
        [clojure.lang.RestFn applyTo RestFn.java 137]
        [clojure.core$apply invokeStatic core.clj 659]
        [clojure.core$require invokeStatic core.clj 5911]
        [clojure.tools.analyzer.jvm$loading__7118__auto____2196 invoke jvm.clj 9]
        [clojure.tools.analyzer.jvm__init load nil 9]
        [clojure.tools.analyzer.jvm__init nil -1]
        [java.lang.Class forName0 Class.java -2]
        [java.lang.Class forName Class.java 348]
        [clojure.lang.RT classForName RT.java 2183]
        [clojure.lang.RT classForName RT.java 2192]
        [clojure.lang.RT loadClassForName RT.java 2211]
        [clojure.lang.RT load RT.java 445]
        [clojure.lang.RT load RT.java 421]
        [clojure.core$load$fn__7645 invoke core.clj 6008]
        [clojure.core$load invokeStatic core.clj 6007]
        [clojure.core$load doInvoke core.clj 5991]
        [clojure.lang.RestFn invoke RestFn.java 408]
        [clojure.core$load_one invokeStatic core.clj 5812]
        [clojure.core$load_one invoke core.clj 5807]
        [clojure.core$load_lib$fn__7590 invoke core.clj 5852]
        [clojure.core$load_lib invokeStatic core.clj 5851]
        [clojure.core$load_lib doInvoke core.clj 5832]
        [clojure.lang.RestFn applyTo RestFn.java 142]
        [clojure.core$apply invokeStatic core.clj 659]
        [clojure.core$load_libs invokeStatic core.clj 5889]
        [clojure.core$load_libs doInvoke core.clj 5873]
        [clojure.lang.RestFn applyTo RestFn.java 137]
        [clojure.core$apply invokeStatic core.clj 659]
        [clojure.core$require invokeStatic core.clj 5911]
        [clojure.core.async.impl.ioc_macros$loading__7118__auto____1596 invoke ioc_macros.clj 12]
        [clojure.core.async.impl.ioc_macros__init load nil 12]
        [clojure.core.async.impl.ioc_macros__init nil -1]
        [java.lang.Class forName0 Class.java -2]
        [java.lang.Class forName Class.java 348]
        [clojure.lang.RT classForName RT.java 2183]
        [clojure.lang.RT classForName RT.java 2192]
        [clojure.lang.RT loadClassForName RT.java 2211]
        [clojure.lang.RT load RT.java 445]
        [clojure.lang.RT load RT.java 421]
        [clojure.core$load$fn__7645 invoke core.clj 6008]
        [clojure.core$load invokeStatic core.clj 6007]
        [clojure.core$load doInvoke core.clj 5991]
        [clojure.lang.RestFn invoke RestFn.java 408]
        [clojure.core$load_one invokeStatic core.clj 5812]
        [clojure.core$load_one invoke core.clj 5807]
        [clojure.core$load_lib$fn__7590 invoke core.clj 5852]
        [clojure.core$load_lib invokeStatic core.clj 5851]
        [clojure.core$load_lib doInvoke core.clj 5832]
        [clojure.lang.RestFn applyTo RestFn.java 142]
        [clojure.core$apply invokeStatic core.clj 659]
        [clojure.core$load_libs invokeStatic core.clj 5889]
        [clojure.core$load_libs doInvoke core.clj 5873]
        [clojure.lang.RestFn applyTo RestFn.java 137]
        [clojure.core$apply invokeStatic core.clj 659]
        [clojure.core$require invokeStatic core.clj 5911]
        [clojure.core.async$loading__7118__auto____1152 invoke async.clj 9]
        [clojure.core.async__init load nil 9]
        [clojure.core.async__init nil -1]
        [java.lang.Class forName0 Class.java -2]
        [java.lang.Class forName Class.java 348]
        [clojure.lang.RT classForName RT.java 2183]
        [clojure.lang.RT classForName RT.java 2192]
        [clojure.lang.RT loadClassForName RT.java 2211]
        [clojure.lang.RT load RT.java 445]
        [clojure.lang.RT load RT.java 421]
        [clojure.core$load$fn__7645 invoke core.clj 6008]
        [clojure.core$load invokeStatic core.clj 6007]
        [clojure.core$load doInvoke core.clj 5991]
        [clojure.lang.RestFn invoke RestFn.java 408]
        [clojure.core$load_one invokeStatic core.clj 5812]
        [clojure.core$load_one invoke core.clj 5807]
        [clojure.core$load_lib$fn__7590 invoke core.clj 5852]
        [clojure.core$load_lib invokeStatic core.clj 5851]
        [clojure.core$load_lib doInvoke core.clj 5832]
        [clojure.lang.RestFn applyTo RestFn.java 142]
        [clojure.core$apply invokeStatic core.clj 659]
        [clojure.core$load_libs invokeStatic core.clj 5889]
        [clojure.core$load_libs doInvoke core.clj 5873]
        [clojure.lang.RestFn applyTo RestFn.java 137]
        [clojure.core$apply invokeStatic core.clj 659]
        [clojure.core$require invokeStatic core.clj 5911]
        [clojure.core$require doInvoke core.clj 5911]
        [clojure.lang.RestFn invoke RestFn.java 436]
        [clj_gatling.report$eval47$loading__7531__auto____48 invoke report.clj 1]
        [clj_gatling.report$eval47 invokeStatic report.clj 1]
        [clj_gatling.report$eval47 invoke report.clj 1]
        [clojure.lang.Compiler eval Compiler.java 6951]
        [clojure.lang.Compiler eval Compiler.java 6940]
        [clojure.lang.Compiler load Compiler.java 7403]
        [clojure.lang.RT loadResourceScript RT.java 374]
        [clojure.lang.RT loadResourceScript RT.java 365]
        [clojure.lang.RT load RT.java 455]
        [clojure.lang.RT load RT.java 421]
        [clojure.core$load$fn__7645 invoke core.clj 6008]
        [clojure.core$load invokeStatic core.clj 6007]
        [clojure.core$load doInvoke core.clj 5991]
        [clojure.lang.RestFn invoke RestFn.java 408]
        [clojure.core$load_one invokeStatic core.clj 5812]
        [clojure.core$load_one invoke core.clj 5807]
        [clojure.core$load_lib$fn__7590 invoke core.clj 5852]
        [clojure.core$load_lib invokeStatic core.clj 5851]
        [clojure.core$load_lib doInvoke core.clj 5832]
        [clojure.lang.RestFn applyTo RestFn.java 142]
        [clojure.core$apply invokeStatic core.clj 659]
        [clojure.core$load_libs invokeStatic core.clj 5889]
        [clojure.core$load_libs doInvoke core.clj 5873]
        [clojure.lang.RestFn applyTo RestFn.java 137]
        [clojure.core$apply invokeStatic core.clj 659]
        [clojure.core$require invokeStatic core.clj 5911]
        [clojure.core$require doInvoke core.clj 5911]
        [clojure.lang.RestFn invoke RestFn.java 457]
        [clj_gatling.core$eval30$loading__7531__auto____31 invoke core.clj 1]
        [clj_gatling.core$eval30 invokeStatic core.clj 1]
        [clj_gatling.core$eval30 invoke core.clj 1]
        [clojure.lang.Compiler eval Compiler.java 6951]
        [clojure.lang.Compiler eval Compiler.java 6940]
        [clojure.lang.Compiler load Compiler.java 7403]
        [clojure.lang.RT loadResourceScript RT.java 374]
        [clojure.lang.RT loadResourceScript RT.java 365]
        [clojure.lang.RT load RT.java 455]
        [clojure.lang.RT load RT.java 421]
        [clojure.core$load$fn__7645 invoke core.clj 6008]
        [clojure.core$load invokeStatic core.clj 6007]
        [clojure.core$load doInvoke core.clj 5991]
        [clojure.lang.RestFn invoke RestFn.java 408]
        [clojure.core$load_one invokeStatic core.clj 5812]
        [clojure.core$load_one invoke core.clj 5807]
        [clojure.core$load_lib$fn__7590 invoke core.clj 5852]
        [clojure.core$load_lib invokeStatic core.clj 5851]
        [clojure.core$load_lib doInvoke core.clj 5832]
        [clojure.lang.RestFn applyTo RestFn.java 142]
        [clojure.core$apply invokeStatic core.clj 659]
        [clojure.core$load_libs invokeStatic core.clj 5889]
        [clojure.core$load_libs doInvoke core.clj 5873]
        [clojure.lang.RestFn applyTo RestFn.java 137]
        [clojure.core$apply invokeStatic core.clj 659]
        [clojure.core$require invokeStatic core.clj 5911]
        [clojure.core$require doInvoke core.clj 5911]
        [clojure.lang.RestFn invoke RestFn.java 551]
        [army.core$eval24$loading__7531__auto____25 invoke core.clj 1]
        [army.core$eval24 invokeStatic core.clj 1]
        [army.core$eval24 invoke core.clj 1]
        [clojure.lang.Compiler eval Compiler.java 6951]
        [clojure.lang.Compiler eval Compiler.java 6940]
        [clojure.lang.Compiler load Compiler.java 7403]
        [clojure.lang.RT loadResourceScript RT.java 374]
        [clojure.lang.RT loadResourceScript RT.java 365]
        [clojure.lang.RT load RT.java 455]
        [clojure.lang.RT load RT.java 421]
        [clojure.core$load$fn__7645 invoke core.clj 6008]
        [clojure.core$load invokeStatic core.clj 6007]
        [clojure.core$load doInvoke core.clj 5991]
        [clojure.lang.RestFn invoke RestFn.java 408]
        [clojure.core$load_one invokeStatic core.clj 5812]
        [clojure.core$load_one invoke core.clj 5807]
        [clojure.core$load_lib$fn__7590 invoke core.clj 5852]
        [clojure.core$load_lib invokeStatic core.clj 5851]
        [clojure.core$load_lib doInvoke core.clj 5832]
        [clojure.lang.RestFn applyTo RestFn.java 142]
        [clojure.core$apply invokeStatic core.clj 659]
        [clojure.core$load_libs invokeStatic core.clj 5889]
        [clojure.core$load_libs doInvoke core.clj 5873]
        [clojure.lang.RestFn applyTo RestFn.java 137]
        [clojure.core$apply invokeStatic core.clj 659]
        [clojure.core$require invokeStatic core.clj 5911]
        [clojure.core$require doInvoke core.clj 5911]
        [clojure.lang.RestFn invoke RestFn.java 408]
        [user$eval15 invokeStatic form-init7134710474813340838.clj 1]
        [user$eval15 invoke form-init7134710474813340838.clj 1]
        [clojure.lang.Compiler eval Compiler.java 6951]
        [clojure.lang.Compiler eval Compiler.java 6940]
        [clojure.lang.Compiler eval Compiler.java 6940]
        [clojure.lang.Compiler load Compiler.java 7403]
        [clojure.lang.Compiler loadFile Compiler.java 7341]
        [clojure.main$load_script invokeStatic main.clj 276]
        [clojure.main$init_opt invokeStatic main.clj 278]
        [clojure.main$init_opt invoke main.clj 278]
        [clojure.main$initialize invokeStatic main.clj 309]
        [clojure.main$null_opt invokeStatic main.clj 343]
        [clojure.main$null_opt invoke main.clj 340]
        [clojure.main$main invokeStatic main.clj 422]
        [clojure.main$main doInvoke main.clj 385]
        [clojure.lang.RestFn applyTo RestFn.java 137]
        [clojure.lang.Var applyTo Var.java 700]
        [clojure.main main main.java 37]]}

Sunday, July 10, 2016

Why I'm writing in Clojure and Ruby

Sandy Metz. Around the 35th minute of http://bikeshed.fm/70

I came from Smalltalk, and I segued to Java a little bit, hated every minute of it, and then fell saved by Ruby. And so I have spent the last 20 some years writing code in languages that had enormous faith in my ability to do the right thing. And along with that enormous faith in my ability to do the right thing, they gave me the freedom of everything.
...

(My summary: She talked to a guy after a conference who asked her how using dynamic language could possibly work, and she answered)

You just trust people. And he said they are not trustworthy. And I said well, they shouldn't write Ruby, they should write Java. I really prefer languages that allow me to shoot myself in the foot if I do but give me the power to do that.
-----

  • Has enormous faith in my ability to do the right thing
  • Give me the freedom of everything
  • You just trust people

These are exactly why I'm writing in Clojure and Ruby.

Saturday, June 4, 2016

How to comment in Clojure and my use case of each approach

There are 3 ways to comment code in Clojure. I personally use all 3 of them depending on the situation.

; [semicolon]
It's similar to // or # in the other languages. Source code from the point you put it until the end of line won't be executed.

I use it for a quick short documentation of the code or when I want to try remove a line quickly. It's the most easy to use one because all the IDEs and text editors support it out of the box.

(comment (expr)) [comment macro]
Use it to wrap expression you don't want it to be run. It's more convenient to use when using with tool to help maintaining balance of the parentheses such as paredit or parinfer.

I use this one a lot in while I'm doing interactive development via repl. I always put the code to execute function the I'm implementing in the same file with the function definition so that I can quickly test out when I change something. I wrap that code with comment macro so that I won't be execute automatically by Clojure. When I want, I send only the inside code to the repl to see what's the output of the function.

#_ reader macro
It comments out an expression that follows it, from opening to closing parens

This one I've just learnt about it recently. It comes in handy when I don't want to care about where is the end of the expression. I just put #_ and thing will behave as I expected. It's great for multiline comment, similar to the comment macro with a less keystrokes.


ClojureBridge is done a better explanation than me

Tuesday, May 10, 2016

[Fixed] WARN Not all child routes satisfy compojure.api.routing/Routing. {:path nil, :method nil}, invalid child routes:

I had this error in my client app that uses compojure-api. The cause of this problem is that I put the static assets routing configuration in the wrong place. We put (route/resources "/") inside defapi which is a place for api routing configurations. So to fix this, we create a defroutes for static assets routing config and merge this defroutes with defapi before giving it to webserver as a ring handler. At the end, the code look something like this.

(ns my.namespace
  (:require [compojure.api.sweet :refer [defapi]]
            [compojure.core :refer [defroutes routes]]
            [compojure.route :as route]))

(defapi app
  ...)

(defroutes site
  ...
  (route/resources "/"))


(defn handler []
  (route site app))

Thursday, February 25, 2016

Interesting use case example for `are` macro in Clojure test

I found an interesting use case for are in Clojure test.

You might have ever written a test for your data conversion logic. E.g.
  • Converting data pulled from third-party web service into your internal schema.
  • Converting data fetched from your database to more mobile friendly schema before sending out.
This is an example snippet of test for such a scenario.

convert-test1 shows the style I had been writing until recently. convert-test2 shows how using are making the test better.

Pros:
  • No more duplication of field values between input and output.
  • No more duplication between output schema and code under test schema.
  • It can be use as a document showing clearly how each individual field is mapped.
Cons:
An error message in convert-test1 is better. It shows clearly which field is wrong while convert-test2 throw out the whole problematic result data.

Updated: I have retested, are does report problem for an individual case as well.

Wednesday, February 10, 2016

[ClojureScript] Run all tests in multiple namespaces in REPL

From ClojureScript wiki, it says
If you have many test namespaces it's idiomatic to create a test runner namespace which imports all of your test namespaces and then invokes run-tests
I personally couldn't get it to work. Though I have managed to make run-all-tests works. This is an example

(ns myns.test.runner
  (:require [cljs.test :refer-macros [run-all-tests]]
            [myns.test.ns1]
            [myns.test.ns2]))

(run-all-tests #"myns.test.*")

[DataScript][Solved] Error: Expected number or lookup ref for entity id, got

When you are trying to use Map specification of Datomic's pull api and you got this error
Error: Expected number or lookup ref for entity id, got ....
This happens because you didn't specify schema for that data attribute. Schema is optional on DataScript but it is required in this case. Add this line to your database, the issue should be fixed
{:your-attribute1 {:db/valueType :db.type/ref}
 :your-attribute2 {:db/valueType :db.type/ref}}

Thursday, October 29, 2015

How to get caller function in Clojure

I posted this question on Clojurians slack channel.
What's clojure equivalent of ruby's `caller` method? http://ruby-doc.org/core-2.2.3/Kernel.html#method-i-caller
To put it another way, if a function is call from multiple places, at runtime, is there a way to print out caller for debugging purpose?
Thanks to Alex Miller. He answered
the Thread class has methods to inspect the stack
(Thread/dumpStack) will just dump that thread's stack to stderr
or (Thread/getStackTrace) gives you an array of StackTraceElement's that you can traverse and do whatever
Basically, we can just use Java's Thread class. To be more precise, getStackTrace is an instance method so we have to call it this way.
(.getStackTrace (Thread/currentThread))
We'll get back Java's array of StackTraceElement class.

You can copy and just use a function I wrote here https://gist.github.com/visibletrap/bdf16dd5596c9a72ec73

Thursday, September 24, 2015

How to clear terminal screen in Clojure

Print some ANSI characters
(print (str (char 27) "[2J")) ; clear screen
(print (str (char 27) "[;H")) ; move cursor to the top left corner of the screen
Credit: http://codereview.stackexchange.com/questions/17603/critique-my-clojure-game-of-life-code

Saturday, April 4, 2015

Start lein repl with test profile

When you try to run lein repl with test profile, you'll get warning/error message similar to this.
$ lein with-profile test repl
Warning: no nREPL dependency detected.
Be sure to include org.clojure/tools.nrepl in :dependencies of your profile.
... 
Error loading clojure.tools.nrepl.server: Could not locate clojure/tools/nrepl/server__init.class or clojure/tools/nrepl/server.clj on classpath: 
Error loading complete.core: Could not locate complete/core__init.class or complete/core.clj on classpath: 
Exception in thread "main" java.lang.ClassNotFoundException: clojure.tools.nrepl.server, compiling:(/private/var/folders/7d/98yf91rn0yz6wbh56h5q7rkh0000gn/T/form-init2792809345387087761.clj:1:1340)
The reason is because unlike default profile, tools.nrepl isn't added as dependency for test profile. You can verify this by comparing the output between lein pprint (or lein with-profile default pprint) and lein with-profile test pprint.

To fix this error, you can use the feature that lein provided combining profile like so;
$ lein with-profile default,test repl
With the command above, lein should start the repl without any errors.

Wednesday, August 27, 2014

Really stop Clojure's infinite loop future with future-cancel

If you like me, executed future-cancel and the future is still running. This blog post is for you.

I suppose your code is something similar to this:
(def f (future (while true (your-function ... ))))
(future-cancel f)
All you have to do as describe in this SO comment is to replace true with (not (Thread/interrupted)). Example:
(future (while (not (Thread/interrupted)) (your-function ... )))
Because future-cancel will send interrupt signal to the future, Thread/interrupted will be changed to true. Now your future-cancel should work as expected.

Sunday, July 20, 2014

Run clojure.test inside Light Table

Simply add a line
(run-tests)
and evaluate source code normally (cmd+shift+enter). See the output in the console.

Monday, March 31, 2014

How to zip in Clojure

In programming, there's time when we have 2 arrays and we want values at the same index of both arrays to have operation on each other producing an array of result. In imperative style, we have to setup a temporary counter variable to store index the current operation is on, increment it until reaching the last value of an array. For example, we want to sum values of each index.

In functional style, there's a function called 'zip' (Haskell, Ruby) which allows us to pair up values of each index preparing to apply operation on each pair later.

In Haskell, there's even 'zipWith' which apply operation on the pair immediately instead of producing an intermediate array.

But when it comes to Clojure, I wasn't be able to find a function in its standard library with the same behavior. The closest I could find is 'zipmap' that returns back a map which is not exactly what I want. We definitely can work around a little bit to get a vector.

But as you can see, it doesn't preserve the order of an original vectors.

The next idea I have is to use 'interleave' and 'partition'


It's kinda work, but the fact that we have to call 2 functions is not so satisfied.

I went look up and found this Stack Overflow answer. Yes. Just use simple 'map'!


I think, I overlooked this solution because in Ruby, the language the I'm most comfortable with, 'map' can only operate on only one array.

Learning that Clojure's map can takes any number of collections is an aha moment for me. So now I can sum elements of more than 2 vectors easily.


Since the second argument can accept any function, creating Clojure records is as easy as this.

Monday, January 20, 2014

Light Table: Stop the execution

Use `Eval: Cancel evaluation for the current client` or `Editor: Disconnect clients attached to editor` from the command bar

credit: How to interrupt eval?

Sunday, October 14, 2012

Weird Clojure build-in function names

Clojure has a rich set of build-in functions for manipulating data structures. Library developers have to name each of them as concise as possible to make them convenient to use. Selecting a word that can describe the whole behavior of each function is a very important step and they did this very well.

But for non-native English speaker that coming from Java and Ruby world, I found that some of the
function names are hard to guess and remember what they do. These are an example list of them.
  • quot
  • assoc
  • dissoc
  • interleave
  • interpose
  • juxt
  • reductions
  • trampoline

Btw, I have a feeling that I remember them now after wrote this blog. :)

Tuesday, October 9, 2012

Clojure Map function

I came across one of the quiz from 4clojure, it asks us to reimplement interleave.
=> (interleave [:a :b :c] [1 2 3])
(:a 1 :b 2 :c 3)
After I solve this quiz with a long solution, I looked at the other people solution. One of the solution that impressed me somewhat similar to this
=> (apply concat (map vector [:a :b :c] [1 2 3]))
(:a 1 :b 2 :c 3)
If we run only evaluate Map part, the result looks like this
=> (map vector [:a :b :c] [1 2 3])
([:a 1] [:b 2] [:c 3])
I had hard time to understand that. I got a question in my head. "Of what reason, why Map function take element of 2 vector (3th, 4th parameters) one by one?" It turns out that this is a Clojure Map function behavior.

Then I realized that the reason why I don't easily get that because all functional programming style syntaxes that I have played with, Map functional only takes 1 argument.

Look at Ruby code.
=> [1, 2, 3].map { |i| i + 1 }
[2, 3, 4]  
But for Clojure's Map function, it takes variable arguments and behave like what I explained earlier.
=> (map inc [1 2 3])
(2 3 4) 
=> (map + [1 2 3] [4 5 6])
(5 7 9)
Another question I had was "Can we archive the same variable arguments behavior with Object-Oriented style?"
The answer is No because we can only calling a method to 1 object. But of course, you can archive this with OO language but in functional way like this.
map(->x,y{ x+y }, [1,2,3], [4,5,6])
This examples might look trivial but if we look at it carefully it represents fundamental differences between OO languages and functional languages. For OO language, our method call is a mechanism of passing message to an object. But for functional language, it's just a function call.

Collectd PostgreSQL Plugin

I couldn't find this link when searching with google https://www.collectd.org/documentation/manpages/collectd.conf.html#plugin-postgresq...