Skip to content

Instantly share code, notes, and snippets.

@safehammad
safehammad / chunking.clj
Last active August 3, 2023 12:15
The REPL session from London Clojure Dojo on 1st Aug 2023 helping us understand chunking and its potential gotchas.
(ns chunking
"This is the REPL session a group of us played with at the London Clojure Dojo on
1st August 2023 with the aim of understanding chunking and its potential gotchas.")
;; Many of Clojure's core functions such as `map`, `filter`, `take` and `drop` produce
;; lazy sequences. The elements of a lazy sequence are only evaluated or "realized" as
;; needed. Although it appears each element is evaluated one at a time, since Clojure 1.1,
;; evaluation is "chunked" such that elements are evaluated 32 at a time, a performance
;; optimisation based on the fact that Clojure's immutable data structures are a so-called
;; "trie" with 32 children per node.
@safehammad
safehammad / explore_bond_titles.clj
Last active February 15, 2021 08:33
Explorations on James Bond film title generation
;; Explorations on James Bond film titles
;; See blog post:
;; Full source code: https://github.com/safehammad/bond-title-generator
(def titles ["Dr No" "From Russia with Love" "Gold finger" "Thunder ball" "You Only Live Twice" "On Her Majesty’s Secret Service" "Diamonds Are Forever" "Live and Let Die" "The Man with the Golden Gun" "The Spy Who Loved Me" "Moon raker" "For Your Eyes Only" "Octo pussy" "Never Say Never Again" "A View to a Kill" "The Living Day lights" "Licence to Kill" "Golden" "Eye" "Tomorrow Never Dies" "The World is Not Enough" "Die Another Day" "Casino Royale" "Quantum of Solace" "Sky fall" "Spectre" "No Time to Die"])
(def tokenised-words (map str/lower-case (mapcat #(str/split % #" ") titles)))
; => ["dr" "no" "from" "russia" "with" "love" "gold" "finger" "thunder" "ball" "you" "only" "live" "twice" "on" "her" "majesty’s" "secret" "service" "diamonds" "are" "forever" "live" "and" "let" "die" "the" "man" "with" "the" "golden" "gun" "the" "spy" "who" "loved" "me" "moon" "raker" "for" "yo

Keybase proof

I hereby claim:

  • I am safehammad on github.
  • I am safehammad (https://keybase.io/safehammad) on keybase.
  • I have a public key whose fingerprint is 4DDE 1E0F 1F38 9006 79EE 0056 7625 4174 3B9A 415F

To claim this, I am signing this object:

@safehammad
safehammad / learn_python.py
Created April 23, 2013 09:58
The beginnings of a Koans framework for interactively teaching Python. Created during a coding session at Python Northwest in April 2013. Much more to do :)
#!/usr/bin/env python3
def introduction():
print('Welcome to the Python teacher. My job is to teach you how to program.')
def learn_print_literal():
"""
The 'print' function in Python is used to print words. You only need to tell
@safehammad
safehammad / tonal_words.py
Created March 30, 2013 13:39
Find "tonal" words in English, or more accurately, heteronyms i.e. two or more words with the same spelling but different pronunciation and meaning. More specifically, this script is interested in the stresses in a pronunciation and finds many "initial-stress-derived nouns" where verbs can be turned into nouns by stressing the first syllable e.g…
from nltk.corpus import cmudict
from nltk.corpus import wordnet
def is_multi_pos(word):
"""Return True if the given word can be used as both a noun and a verb.
With a small tweak, you can find words which can also be used as an adjective, or
any combination of the above.
"""
@safehammad
safehammad / tennis.py
Created December 23, 2012 16:06
Tennis scorer.
#!/usr/bin/env python3
"""Tennis scorer developed during Python Northwest coding meeting Dec 2012.
Given a current score and the winner of the next point, the script
will return the next score.
Usage: python3 tennis.py <p1_score> <p2_score> <scorer>
Output: <p1_score> <p2_score>
@safehammad
safehammad / tennis.sh
Created December 20, 2012 20:33
Tennis Score Python Script Tester
#!/bin/bash
# Tennis Score Python Script Tester
#
# Usage: tennis <python_script>
#
# Args passed to script: <score_player_1> <score_player_2> <next_point_won_by>
# New score expected: <score_player_1> <score_player_2>
#
# Where <score_player_n> is one of 0, 15, 30, 40, ADV, WIN