Saturday, 21 November 2015

Vartalaap

यार मुझसे कहता है के
सुन्ने वाला भी मैं हूँ  |
अंदर देखो, आँखें खोलो,
रूह और परछाई मैं हूँ  ||

जंगल के हर फल में मैं हूँ,
तक़दीर के हर पल में मैं हूँ  |
सावन के हर कल में मैं हूँ,
रेशम और मलमल में मैं हूँ  ||

- . -

yaar mujhse kehta hai ke
sunney wala bhi main hoon |
andar dekho, aankhein kholo,
rooh aur parchhai main hoon ||

jangal ke har phal mein main hoon,
taqdeer ke har pal mein main hoon |
saavan ke har kal mein main hoon,
resham aur malmal mein main hoon ||

Saturday, 1 August 2015

Go Play

I've been reading up on core.async lately, mainly because of its shared semantics with Go and alleged benefits of the CSP model.

The literature around core.async is a little sparse, though it is simple enough to pick up.
The main benefits (as I understand them) are:
  1. Uses lightweight threads for concurrency
  2. Redeems asynchronous code from callback hell 
Time to see it in action:

(require '[clojure.core.async :refer [chan go alt! >!]])

(def even (chan))
(def odd  (chan))

(go (while true
  (alt!
    even ([v] (println "Got" v "from even"))
    odd  ([v] (println "Got" v "from odd")))))

(go (doseq [i (range 0 10 2)]
      (>! even i)
      (Thread/sleep (rand-int 1000))))

(go (doseq [i (range 1 10 2)]
      (>! odd i)
      (Thread/sleep (rand-int 1000))))

Here's what's happening above:
We create two 'channels' - even and odd
Channels are very much like queues or conveyor-belts in that we can insert values on one end and retrieve values from the other.
We then create three asynchronous processes using go blocks -
  1. The first process has an infinite loop that 'listens' to our channels using alt!. If either of the channels has received a value, it prints some information to the console.
  2. The next two processes produce numbers from 0-9 at random intervals and put them on corresponding channels using the >! operator.
..and this is enough to get started!

In the real world, we can use channels to listen to changes in state such as mouse moves and key events, and update state accordingly. I might write a game using core.async soon.

This was fun!
---

EDIT: Find this and other core.async examples here.

Monday, 20 July 2015

Prime?

Today I found something called Wolstenholme’s theorem, which says: 
For a prime p > 3, the numerator of H(p-1) is divisible by p^2,
where H(n) =  1/1 + 1/2 + ... 1/n

Here's a function (in Clojure) to test whether a number is a prime based on the above:

(defn harmonic [n]
  (apply + (for [i (range 1 (inc n))] 
             (/ 1 i))))

(defn possibly-prime? [n]
  (if (< n 4)
    (or (= n 2) (= n 3))
    (-> n dec harmonic numerator (mod (* n n)) zero?)))

PS: Thank you Colin Wright for the corrections.

Friday, 10 July 2015

Them

They were puppet masters of the first order, 
with thick supply of slimy hands that rose menacingly from their pulsating innards
and shone dimly with viscous Ichor. 
They did what they wished to do - destroyed what they disliked,
reproduced what they thought proper.

They had countless faces, each more captivating than the last, 
with searing fury and wrath itching behind their deceptively thin hides. 
They consumed the finest and the filthiest, 
growing twofold with each burp
 that made hills with frozen tops shiver and shudder.

They had a billion eyes that were almost always watching. 
They saw but didn't observe at times, and at times tortured entire communities. 
What they sought, we can only wonder, but it was not to be found here,
amidst sprawling meadows and bubbling brooks, 
for they are antitheses.

They came from somewhere deep in space. 
They poisoned our environment, spread vicious diseases, mutilated our land, 
and gave us gold and fish for our troubles. 
They never went back - just took to resting 
in this hopeless nauseous world they had created and seemed to tolerate.

Saturday, 4 July 2015

Jadugarni Alisha

I was supposed to go down to Hardoi to deliver some papers to the bank. I was seated comfortably in the cab, reading an interesting article about the Gita on the daily.

Hardoi is a small town in the backwaters of Uttar Pradesh and is a three hour drive away from Lucknow. I had been there once or twice before quite some time back, and remembered it as a queer old laidback town with decaying Mughal architecture and big, sprawling gardens. Someone had told me it was where the story of Hiranakashyap and Prahlad was based wherein the snobby king had been murdered by Lord Vishnu in his Narsimha avatar, ending his wicked rule.



As soon as I was free from the bank, I went to a small, bustling eatery and had a plate of puri-bhaji and a serving of the famous Sandila laddus. Afterwards, I strolled around the market smoking a cigarette I had bought at a paan shop on the previous crossing, paan in mouth.

After wandering around for some time, I saw a vendor selling haula peanuts in front of a dilapidated old building, and went to have a look. After asking the man to pack a good helping, I noticed a poster on the wall behind him. A crowned golden fish on a red backdrop was painted on the top left corner.’Jadugarni Alisha’, it said in bright rainbow colours- ‘Daily shows at 11:00 am, 1:00 pm, and 5:00 pm, Dream Theater’.

This piqued my interest, I took a look at my phone for the time. It was half past four and I was wondering whether I should go. Having nothing else to do and a heightened curiosity, I decided on a whim - “What is the way to Dream Theater, bhaiya?”



I bought a ticket at the counter, and entered a small dim-lit hall with shambly seating for twenty to thirty people, and a circular dais on one end. The red wall behind the dais was adorned with the same golden fish from the poster. I was alone and took a seat in the second row on the right, lighting another cigarette and opening the bag of peanuts.

By the time it was 5, eight to ten other people had come and were sitting haphazardly around the place. A light focused on the dais came up, and the rest of the room became pitch-black. A low drumming sound started echoing through the room, and a girl in her twenties wearing a large cloak and red hat with the fish entered the spotlight and stepped up on the dais. She had bronze skin and from her looks I guessed she was Bengali.
Jo dikhe aaj yahan, shak se naa dekhein usey. 
Ye aankhon ka dhoka nahi, ufanta sach hai.
(Do not doubt the authenticity of what you see here today.
This is revelation, not illusion.)

I was a little unnerved by this. I had expected an under-educated small-town girl doing run-of-the-mill tricks, but here she was - talking mystically in poems. I was probably frowning at this point, and I imagine I saw a hint of a smile when she looked my way.

I turned to look at the other people in the audience, but the hall was too dark and my eyes had adjusted to the light in front of me. I straightened up a bit and redirected all my attention towards this mysterious being.

She was now standing with her back to the audience, arms spread out like The Redeemer, head tilted back a little.
Ia!
The light falling on the podium started flashing, taking various colours. The room was silent and had an intense atmosphere. I could hear, or thought I could hear crickets chirping in the darkness around me.
Ia! Shub-Niggurath!
This incantation caused a flurry of confused emotions in me, and I could feel my stomach sink. Had I ever heard it before? I probably had, in whispered voices around a bonfire in Manali, from a group of Syrians who had travelled along the Rub’ al Khali.
Ia! Yog-Sothoth!
The lights were dancing like a whirling Dervish.
Mrit nahin voh - jo sadiyo jama hai,
Kuchh sadiyo mein toh maut bhi fanaa hai.
(That is not dead which can eternal lie,
And with strange aeons even death may die.)

The noise of chirping crickets had gained a startling intensity, and was ringing sharply in my head. It seemed to be malevolent and mocking, like shrill laughter echoing in a cave.
Yog-Sothoth dwar hai, aur Yog-Sothoth kundi.
Sab samay aur sthaan Yog-Sothoth mein ek hain.
(Yog-Sothoth is the gate, and Yog-Sothoth is the key.
Time and space are one in Yog-Sothoth.)
Aieei-k’tay, Yog-Sothoth, 
Aieei-k’tay!
Her arms were flapping around as if they were made of rubber. They seemed to be loosing shape, and taking on a glossy, slimy texture. Her hair were flying about, but there was no wind. Crickets were threatening to rupture my eardrums.
Ia! Ph’nglui mglw’nafh Cthulhu R’lyeh wgah’nagl fhtagn!
These words had been incanted by her - that I do not doubt one bit - but not in her orotund, fruity voice. It was the sound of those crickets shrieking inside my mind.

Then, right in front of me, the large cloak and hat fell to the ground in a heap. What I saw then, I will never forget. At first, I thought they were bees - swarming where the girl had just been. I wanted to believe they were bees.

But they were globules of floating light of every color known and unknown. They were bubbling among themselves - constantly in motion and very alive. I have no other means to describe it except as turbulent energy frothing forth from a slit in space.

Amidst that maddening sight, I saw Ape become Man. I saw Krishna in his divine form driving Arjuna to war, singing his Song. I saw Man kill Man. I saw tanks and rockets blow the earth to smithereens. I saw the Ocean turn scarlet and get vanquished by the insatiable Desert. I saw ancient temples of unknown deities sink to the deepest depths of space and age. I saw hideous, disembodied tongues relish rotting flesh. I saw sheep devouring humans, trees causing quakes, and stars distorting space.



I was sweating and shivering feverishly by the time I came to. The dais was vacant, the lights were on, and there was no one else in the hall.

Thursday, 28 May 2015

Yowl

Mumba whose eyes are a thousand blind windows! Mumba whose skyscrapers idle in the streets like apathetic giants! Mumba whose buildings are judgment! 
Mumba whose mind is pure machinery! Mumba whose blood is running money! Mumba whose clogged veins threaten to rupture!
Mumba whose love is endless oil and stone! Mumba whose soul is electricity and banks! Mumba whose factories dream and croak in the smog!
Mumba whose poverty is the specter of genius! Mumba the incomprehensible prison! Mumba whose fate is a wave of the waiting sea!

-- (with due credit to Allen Ginsberg)

Sunday, 11 January 2015

Feeding the Shoggoth

There is something about H.P. Lovecraft. He gets to your head. 



~*~
"The oldest and strongest emotion of mankind is fear, and the oldest and strongest kind of fear is fear of the unknown."
~*~

The plot of his stories is largely inconsequential, and his characters show little emotion aside from those of fear and disbelief. It's the after-taste that's delicious. That there are beings amongst us that are far beyond the comprehension of our senses, beings that have lived for aeons and traveled long through space and time, is a possibility that strikes the conscience in an ungodly manner and lingers in one of its many corners forever after.

The question is- what was he driven by?

Many people have attempted answering this question, and they seem to focus too much on his parents' mental condition. I believe there was a deeper reason behind all of it- he had lost his faith, but did not want to 'come out'.


~*~
"If religion were true, its followers would not try to bludgeon their young into an artificial conformity; but would merely insist on their unbending quest for truth, irrespective of artificial backgrounds or practical consequences."
~*~

He feared that if he publicly renounced his faith, he would be deemed one of those slow, primitive, racist stereotypes that he described so much in detail. 

He was torn between civilization and science.

He could not bring himself to believe in what his priests told, and he asked himself- "Why should I believe them? I myself have seen no proof."

And so he set out on a social experiment to see what he could get others to believe, and more importantly, fear. He thought of cults that had their own beliefs, their own gods- gods that were as probable to exist as the ones he'd been told about.

And out of this social experiment, came the Cthulhu Mythos.

~*~
"Bunch together a group of people deliberately chosen for strong religious feelings, and you have a practical guarantee of dark morbidities expressed in crime, perversion, and insanity."
~*~

The other religion that he thought of taking to should arguably have been morphine, as apparent through his writings. He feared that he would get addicted to it, and it would lead him to madness.

But, curious as he was, he fantasized about the apparent delirium brought on by opium, and others' perception of him in that situation, and wrote the extremely detailed emotions that he imagined everyone would experience.

Thus, The Mythos, though born out of a fear of madness, were inspired by his disbelief in religion and the desire to take to drugs to escape this life that he so disliked- in true Goth fashion. 

In fact, I would argue that Lovecraft's ideas brought on the Beatnik movement. Burroughs said this on coming across a real (fake) Necronomicon:

~*~
"The deepest levels of the unconscious mind where the Ancient Ones dwell must inevitably surface for all to see. This is the best assurance against such secrets being monopolized by vested interests for morbid and selfish ends." 
~*~

Thus, the Beats saw their inner demons reflected in these strange, indifferent gods, and the struggle to be out and open with them before they became weapons in the hands of others, came to be a central, fundamental principle of their philosophy.

H.P. Lovecraft was awesome.

Wednesday, 7 January 2015

Maya- A DSL for math and numerical work

I feel awkward writing mathematical functions as s-exps

(defn quadratic-1 [a b c]
  (let [d (* 4 a c),
        D (Math/sqrt (- (* b b) d)),
        t (* 2 a), -b (- b)]
    [(/ (+ -b D) t) 
     (/ (- -b D) t)]))

Clojure's thread-first macro eases the pain a bit..

(defn quadratic-2 [a b c]
  (let [d (* 4 a c),
        D (-> (* b b) (- d) Math/sqrt),
        t (* 2 a), -b (- b)]
    [(-> -b (+ D) (/ t)) 
     (-> -b (- D) (/ t))]))

..but it's still pretty loaded with parens, and not very clear.

We need a math DSL that looks infixy and uses fewer parens.
Luckily, we can hack one for ourselves in no time.

~*~

Step 1- Thread mathematical expressions

(defmacro math->
  "(math-> 1 + 5 * 2 / 3) ;=> (-> 1 (+ 5) (* 2) (/ 3)) ;=> 4" 
  [exp & f-x-pairs]
  (if (even? (count f-x-pairs))
    `(-> ~exp 
       ~@(for [[f x] (partition 2 f-x-pairs)]
           (list f x)))
    (throw (Exception. "f-x-pairs should be even."))))

Step 2- Allow temporary bindings

(defmacro maya
  "(maya 1 + 5 :as six, six * 2 :as twelve, twelve / 3 * 2) ;=> 8"
  [& exprs]
  (let [[exp [_ ?as & next-exprs :as E]] (split-with #(not= :as %) exprs)]
    (if (empty? E)
      (cons `math-> exp)
      `(let [~?as (math-> ~@exp)]
         (maya ~@next-exprs)))))

Step 3- Profit?

(defn quadratic [a b c]
  (maya 4 * a * c :as d,
        b * b - d -> Math/sqrt :as D,
        2 * a :as t, (- b) :as -b,
        -b + D / t :as x1,
        -b - D / t :as x2,
    [x1 x2]))

~*~

Edit: Here's the original gist.