I was experimenting with some sequences today, and ran into a stumbling block: Using immutable data structures, how do you execute multiple transformations in series on an object, and return the final value?
For instance, consider a sequence of numbers,
user> (range 90 100)
(90 91 92 93 94 95 96 97 98 99)
How do you transform them such that you increment each number by 1, and then get their text representation,
"^_`abcd"
Imperatively speaking, you would run a loop on each word, and transform the sequence data structure in place, and the last operation would achieve the desired result. Something like,
>>> s = ""
>>> a =
>>> a
>>> for i in range(0,len(a)):
... s += chr(a+1)
...
>>> s
'^_`abcd'
If you knew about maps in python, this could be achieved with something like,
>>> ''.join()
'^_`abcd'
The easiest way to do this in Clojure is using the excellently named (http://debasishg.blogspot.com/2010/04/thrush-in-clojure.html)(-> and ->>). According the doc,
Threads the expr through the forms. Inserts x as the
second item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
second item in second form, etc.
It is used like this,
user> (->> (range 90 100) (map inc) (map char) (apply str))
"^_`abcd"
Basically, the line, (-> 7 (- 3) (- 6)) implies that 7 be substituted as the first argument to -, to become (- 7 3). This result is then substituted as the first argument to the second -, to get (- 4 6), which returns -2.
user> (-> 7 (- 3) (- 6))
-2
Voila!
--
If you have any questions or thoughts, don't hesitate to reach out. You can find me as @viksit on Twitter.
From the (http://www.reuters.com/article/idUSTRE6341EA20100506),
The Dow suffered its biggest ever intraday point drop, which may have been caused by an erroneous trade entered by a person at a big Wall Street bank, multiple market sources said.
and the suspected cause? A UI Glitch!
In one of the most dizzying half-hours in stock market history, the Dow plunged nearly 1,000 points before paring those losses—all apparently due to a trader error.
According to multiple sources, a trader entered a “b” for billion instead of an “m” for million in a trade possibly involving Procter & Gamble , a component in the Dow. (CNBC’s Jim Cramer noted suspicious price movement in P&G stock on air during the height of the market selloff. Watch.)
Sources tell CNBC the erroneous trade may have been made at Citigroup .
“We, along with the rest of the financial industry, are investigating to find the source of today’s market volatility,” Citigroup said in a statement. “At this point we have no evidence that Citi was involved in any erroneous transaction.”
According to a person familiar with the probe, one focus is on futures contracts tied to the Standard & Poor’s 500 stock index, known as E-mini S&P 500 futures, and in particular a two-minute window in which 16 billion of the futures were sold.
Citigroup’s total E-mini volume for the entire day was only 9 billion, suggesting that the origin of the trades was elsewhere, according to someone close to Citigroup’s own probe of the situation. The E-minis trade on the CME.
--
If you have any questions or thoughts, don't hesitate to reach out. You can find me as @viksit on Twitter.
Its surprising that the modulus (%) operator in C++ works upwards, but not downwards. When working on some code, I expected,
-1 % 3 = 2
0 % 3 = 0
1 % 3 = 1
2 % 3 = 2
but ended up with,
-1 % 3 = -1
0 % 3 = 0
1 % 3 = 1
2 % 3 = 2
As a result, you’d need to ensure that either you check that your result is
result = n % 3;
if( result
Or, a better solution might be to change the expression such that the negative case never arises,
Hope this helps someone out there!
--
If you have any questions or thoughts, don't hesitate to reach out. You can find me as @viksit on Twitter.
I was trying to implement a simple binary search using a purely functional approach, and after much hacking, googling and wikibooking, came up with this in Clojure.
--
If you have any questions or thoughts, don't hesitate to reach out. You can find me as @viksit on Twitter.
I was recently looking for a method to create an application with Clojure that would allow specification of command line arguments.
I came across an excellent (http://stackoverflow.com/questions/1341154/building-a-clojure-app-with-a-command-line-interface) on Stack Overflow by (http://stackoverflow.com/users/128927/alanlcode), that provides a spectacular example. I’ve (http://gist.github.com/367681) it for reference.
--
If you have any questions or thoughts, don't hesitate to reach out. You can find me as @viksit on Twitter.