View clojure-tricks.clj
(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.
View csv-load-and-process.clj
; 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 |
; +----------------------------------------------------------+
;
View kcombinations.groovy
List kCombinations(List l, Integer k) {
assert k >= 0, 'Invalid "k"'
if (l.empty || !k) []
else if (k == 1) l.collate(1)
else
(0..l.size()-1).inject([] as Set) { acc, i ->
def c = kCombinations(l.drop(i+1), k-1)
acc + [l[i], c].combinations { cn ->
def cnf = cn.flatten()
cnf.size() == k ? cnf : [l[i],c].flatten()
View ListCombinations.groovy
/*
* Author: Bahman Movaqar <Bahman AT BahmanM.com>
* Copyright (c) Bahman Movaqar
* This file is released under public domain license.
*/
///// Tests below the main class
import groovy.transform.PackageScope
/**
View simple-constraint-problem.groovy
/*
* Solve the equation A + B = CD - E = FG
* WHERE A..G in [0,1,2,3,7,8,9]
* AND CD = 10*C + D
* AND FG = 10*F + G
* AND C not 0 and F not 0
* AND different letters are different digits
*/
[0,1,2,3,7,8,9].permutations().findAll {
View test-runner.el
;;; A quick way to run JUnit tests:
;;; `C-x t u` runs all the test cases in a unit.
;;; `C-x t c` runs a single test case.
;;;
;;; Simply add the snippet at the end of `init.el` or `.emacs`.
;;; NOTE: It assumes you are using projectile to manage the project.
;;;
;;; By Bahman Movaqar
;; runs all test cases in the current class
View lazy-list-combinations.scm
; 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)
View grab-packt-racket.rkt
#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)
View grab-packt.groovy
/**
* @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
View list-group-by.ml
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;;