Saturday, December 15, 2012

Manipulating vector of maps - Clojure.

So here is a vector of maps.
Map contains name, age and school as keys. Note that school is an optional key.

> (def student-grades
[{:age 21, :name "Mayank", :school "MSB"}
{:age 23, :name "Alex"}
{:age 11, :name "Amit", :school "Ajmer"}])

Now I would like to take that vector of maps and return the same name, increment the age and if school key is missing return "-" as its value else return the school itself.

So I write a function which take one map like that. and de-structure its keys.

> (defn get-grades
  "This will take a map of name age school as keys and return a map of name, incremented age and if school is not given then - else the name of the school itself."
  [{:keys [name age school]}]
  {:name name
   :age (inc age)
   :school (or school "-")})


And now All I have to do is map that function onto that whole vector of maps and we get :

> (map get-grades student-grades)
({:name "Mayank", :age 22, :school "MSB"}
 {:name "Alex", :age 24, :school "-"}
 {:name "Amit", :age 12, :school "Ajmer"})



Notice age has been incremented, and if school is empty it is returning a "-" :)


This took me sometime to grasp but somehow I just got it. So thought i'll share it down.

Reference :  http://blog.jayfields.com/2010/07/clojure-destructuring.html

Saturday, October 13, 2012

Activity 1-16 : TECS

This was pretty cool. I have to admit. I had to look at some answers. Don't know why but I got stuck at DMux4Way :-/
But this was some learning experience.
Moving on :)

Reference : http://diycomputerscience.com/courses/course/the-elements-of-computing-systems/section/designing-elementary-logic-gates-from-a-nand-gate-using-hardware-description-language

Thursday, September 6, 2012

A Tale of Computer Engineering.

For 4 years, I learnt everything about water. It's properties, the techniques to swim, history of all the swimmers. Oh I touched the water all right! 

I know how to check the temperature, know all the gears and the cool sign language to communicate inside water. Of course we practiced it on land not inside water. That would be too dangerous.

I had the most knowledge about water among my peers. I knew it's exact chemical properties and how it behaves in various conditions. Just everything a best swimmer should know of.

My training was complete after 4 years.

The next day, they took me to the ocean. And said,

"Jump".

"What? Like now? But I have never swum before so deep. In fact I have only swum in a controlled environment.", I said

They threw me into the ocean.
 
I gasped.
I cried.

"I don't know shit about swimming but!! Help me", I shouted. I cried out loud.

Other Swimmers were laughing around me.
 
"Welcome to the real world", they said.

"You don't know what swimming is like, even after so many years of so called learning.

"I'll tell you one little secret, kid. You learn swimming by being inside water. You learn swimming, by swimming. There is no other way. There is no easy way. You swim and you learn. You learn by swimming."

And slowly slowly I unlearned what I had already learned.

Lesson for life : You learn by doing.

Wax on, Wax off. 
Wax off, Wax on. 


Mr. Kesuke Miyagi: First, wash all car. Then wax. Wax on... 

Daniel LaRusso: Hey, why do I have to...? 

Mr. Kesuke Miyagi: Ah ah! Remember deal! No questions! 

Daniel LaRusso: Yeah, but... 

Mr. Kesuke Miyagi: Hai! [makes circular gestures with each hand] Wax on, right hand. Wax off, left hand. Wax on, wax off. Breathe in through nose, out of mouth. Wax on, wax off. Don't forget to breathe, very important. [walks away, still making circular motions with hands] Wax on... wax off. Wax on... wax off.

List of MUST watch talk for a computer scientist

Each link/video is a Gem. I'll add more videos to this list as I find them.
Enjoy!

Bret Victor talks about the idea of inventing something our of your own principles, not for money, or to make a mark, but because you have some principles and you want to share it with others. Very profound talk.
Bret Victor - Inventing on Principle



Rich is the author of Clojure, A lisp dialect which is on the rise and looks like will change the way we develop. It is different from the other lisps and yet is lisp in its purity.
While this talk has nothing to do with clojure. it is about how you do solve the most difficult problems that you ever encounter. How do you crack the hardest problems of the all. Really inspiring.
Hammock Driven Developement - Rich Hickey

This is another profound talk by Rich (nothing to do with Clojure) where he talks about how we got programming all wrong.
Are we there yet? - Rich Hickey

People often choose Easy over Simplicity while thinking that something that is easy is also simple which is not the case. A Beautiful talk about what is easy and what is simple and how making things simple is the way to go - Rich Hickey (nothing to do with clojure again)
Simple Made Easy

We often program with variables and in place. This was a beautiful talk about how we got things wrong when we think in terms of variables as the key. Rich really nails the idea that we should be not doing any Place oriented programming. As to what really place oriented programming is, you will have to see the talk :)
Value of values by Rich Hickey


This talk is about Strings, Numbers and Time. And how we got it all wrong.
Jon Skeet and Tony the Pony

Again a profound talk by Uncle Bob. It has a lot to with one of the key ideas of Lauren Ipsum
Twenty Five Zeroes

[Clojure] how to setup local git repo as dependency in lein?

<gerry`> how to setup local git repo as dependency in lein?

<gerry`> or local clojure***.jar as dependency?

<xeqi> gerry`: are you building clojure from source and want to use it as a dependency in lein?

<gerry`> xeqi: right.

<xeqi> run `mvn install` in your local clojure checkout.  That will put [org.clojure/clojure "1.5.0-master-SNAPSHOT"] into your local ~/.m2 cache which lein can then use

<xeqi> assuming your on a recent clone

<xeqi> and didn't change the groupId, artifactId, or version in clojure's pom.xml

<gerry`> and use [org.clojure/clojure "1.5.0-master-SNAPSHOT"] as dependency in my project.clj file?

<xeqi> yes

<gerry`> ok,thx

<firesofmay> xeqi, will this work for other libraries as well (written in clojure)? or is this only for building clojure language itself?

<firesofmay> xeqi, and won't this conflict if I want to use the original library as well?

<xeqi> firesofmay: if the dependency is a maven or lein project `mvn install` or `lein install` will work.  otherwise if you just have a free floating jar something like lein-localrepo will be needed

<xeqi> trying to use 2 vesions of the same library in the same project would conflict anyways

<xeqi> ~repeatability

<clojurebot> repeatability is crucial for builds, see https://github.com/technomancy/leiningen/wiki/Repeatability

<firesofmay> xeqi, i mean in different projects. if i want to test my new version in  a new project and still use old version in other project i.e.

<xeqi> firesofmay: that works fine.  The jars end up in ~/.m2 under a groupid/artifactid/version scheme

<firesofmay> xeqi, okay thanks for answering.

<xeqi> so as long as you don't use install a jar with the same version as an already installed jar, the ~/m2 cache has both

<firesofmay> xeqi, okay.

Tuesday, September 4, 2012

[Clojure] Difference between future and promise

The difference between future and promise can be quite confusing. Heck, even I am confused. So I went and asked at #clojure @freenode

<firesofmay> Can anyone tell me why is Promise called as "Promise"? What is the context? I understand (a bit) what they do. But I am trying to understand the reason to name it that way.

<weavejester> firesofmay: You're promising to assign it a value at a later date.
 

<firesofmay> weavejester, ah okay. and future is like, this function call will return its value sometime in future. Am I correct?
 

<gfredericks> a future is like a promise where you give it the computation to produce the result at the same time you create the future itself
 

<weavejester> firesofmay: Yes… but more precisely, what gfredericks said :)
 

<gfredericks> promises decouple that; you create a promise without saying anything about who computes its value or how
 

<weavejester> A future is essentially a promise + a calculation in a background thread that fulfills the promise.
 

<firesofmay> gfredericks, ah that clears the difference.
 

<firesofmay> weavejester, now I got it :)

Hope that helps someone :)

Thanks to  gfredericks & weavejester.

Reference Links :
1) How do Clojure futures and promises differ?

2) Episode 1 - Futures & Promises

Sunday, September 2, 2012

Question and Answer.

Again and again I see people writing questions and answers in a really bad way. And I am not talking about using fancy words. People fail to structure their answers/questions in a way that it is easy for others to understand.

And when you counter them, they say - "Don't take it so seriously, it's just internet. Or it's just a question/answer. Get a life!"

What people fail to see is that they will never be able to reach the highs which other masters have achieved if they write like that.

And what is common among all the masters in every field? They are excellent communicators. They can communicate the most complicated ideas to anyone in the most simplest manner without fail.

You see, when you write a question, not only you are asking something, you are sharing something about yourself. The way you write a question, speaks of how deeply you understand the problem, how passionate you are about that problem. How many battles you have fought and failed before  asking this question.

If someone fails to express what his/her problem is clearly, it simply means, he/she does not understand the problem itself. And thus is just wasting our time.

The same goes with answers. When you write an answer, not only you are helping that person out, you are learning What is the right way to express an answer such that not only it solves that problem, it helps you understand the internal thought process of how you break down a solution.

Think before you Ask/Answer someone somewhere. Choosing your words correctly can make a lot of difference.

Thought Reference : http://www.codinghorror.com/blog/2011/02/how-to-write-without-writing.html