Skip to content

Instantly share code, notes, and snippets.

View bahmanm's full-sized avatar

Bahman Movaqar bahmanm

View GitHub Profile
@bahmanm
bahmanm / simple-atoi.java
Created June 20, 2023 23:30
Nested or sequential try-catch blocks?
/**
* Using nested try-catch blocks.
*
* @param str a string representing an integer
* @return smallest integer type or null
*/
Number atoi_NestedTryCatch(String str) {
try {
return Integer.valueOf(str);
} catch (NumberFormatException ignore1) {
@bahmanm
bahmanm / my-custom-config-loader.el
Created June 15, 2023 20:39
eLisp: Save and parse JSON
;; to save an object into a json file
(with-temp-buffer
(json-insert '(:foo "bar"))
(write-file "~/tmp/my.json"))
;; load a json file as hash table
(with-temp-buffer
(insert-file-contents "~/tmp/my.json")
(json-parse-buffer))
@bahmanm
bahmanm / list-group-by.ml
Created November 20, 2015 17:40
OCaml `List.group_by`
let group_by (f : 'a -> 'b) (ll : 'a list) : ('b, 'a list) Hashtbl.t =
List.fold_left
(fun acc e ->
let grp = f e in
let grp_mems = try Hashtbl.find acc grp with Not_found -> [] in
Hashtbl.replace acc grp (e :: grp_mems);
acc)
(Hashtbl.create 100)
ll;;
@bahmanm
bahmanm / gradle-init.sh
Last active January 30, 2019 09:00
Groovy and Spock (with Gradle)
#!/usr/bin/env bash
###
# Initialises a Gradle project with Groovy and Spock.
#
# How To Use?
#
# 1. Save the file, e.g. `gradle-init.sh`
# 2. Make it executable: `$ chmod +x gradle-init.sh`
# 3. Initialise the project: `$ ./gradle-init.sh YOUR_PROJECT_NAME`
@bahmanm
bahmanm / shapeofthedate.groovy
Last active November 17, 2017 20:58
Convert dates to alien looking plots using partial sums - https://github.com/bahmanm/datedreamer
//
// This has evolved into a fully-fledged project called DateDreamer.
// Please check the project's page for releases and more information.
// https://github.com/bahmanm/datedreamer
//
@Grab(group='com.github.yannrichet', module='JMathPlot', version='1.0.1')
@Grab(group='org.apache.commons', module='commons-math3', version='3.6.1')
import static java.lang.Math.*
import org.apache.commons.math3.complex.Complex
/**
* @author Bahman Movaqar <Bahman AT BahmanM.com>
*/
@Grab('org.jsoup:jsoup:1.8.2')
import static org.jsoup.Jsoup.parse
def cookieManager = new CookieManager(null, CookiePolicy.ACCEPT_ALL)
CookieHandler.setDefault(cookieManager)
def doc = parse(
new URL('https://www.packtpub.com/packt/offers/free-learning').text
#lang racket
;;;
;;; Author: Bahman Movaqar <Bahman AT BahmanM.com>
;;;
(require net/url)
(require sxml)
(require net/uri-codec)
(require net/http-client)
(require (planet neil/html-parsing))
(require net/cookies)
@bahmanm
bahmanm / clojure-tricks.clj
Created April 15, 2012 12:48
Clojure tricks and useful functions/macros
(defn getfn [fname]
"Gets a function by its fully qualified name."
(let [fsym (symbol fname)
nsym (symbol (namespace fsym))]
(require nsym)
(resolve fsym)))
(defn interpolate [s vals]
"Returns the interpolation of string 's' with a map of key/values."
;Keys in 's' are marked with #{key-name} syntax.
@bahmanm
bahmanm / csv-load-and-process.clj
Created August 13, 2012 18:33
Processing CSV contents
; Suppose you have a CSV file with the first row being the column names
; and the rest being data. Using the following snippet you can process
; the contents passing each row's contents as keyword arguments to
; another function.
;
; Example CSV structure:
; +----------------------------------------------------------+
; | customer-code | name | e-mail | address | phone |
; +----------------------------------------------------------+
;
@bahmanm
bahmanm / lazy-list-combinations.scm
Created January 4, 2015 14:25
Computes the combinations of any number of given lists as a lazy sequence.
; Copyright Bahman Movaqar <Bahman AT BahmanM.com>
; Source -> https://github.com/bahmanm/touka/blob/master/misc.scm
; Tests -> https://github.com/bahmanm/touka/blob/master/tests/misc-tests.scm
;; Collects the CAR of each list in the given list of lists!
(define (cars list-of-lists)
(map car list-of-lists))
;; Collects the CDR of each list in the given list of lists!
(define (cdrs list-of-lists)