Skip to content

Instantly share code, notes, and snippets.

View gdevanla's full-sized avatar

Guru Devanla gdevanla

View GitHub Profile
@gdevanla
gdevanla / pandas-0.13-bug.ipynb
Created October 9, 2015 04:58
pandas-0.13-bug
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import pandas as pd
import numpy as np
class InsertContext():
def __init__(self, tf):
self._tf = tf
self.__write_mode_on = False
def __enter__(self):
// code generated from <org.scribe.extractors.TokenExtractorTest: void shouldExtractTokenFromOAuthStandardResponse()>
com.ser.assist.statecarver.xstreamcarver.StaticStateLoader.loadStaticState(760, "scribe-java_freqT")
def dbName_intg = "scribe-java_freqT_intg"
temp$0 = com.ser.assist.statecarver.xstreamcarver.XStreamStateCarver.loadState(760, 200, "scribe-java_freqT")
temp$0_clone = com.ser.assist.statecarver.xstreamcarver.XStreamStateCarver.loadState(760, 200, "scribe-java_freqT")
response = com.ser.assist.statecarver.xstreamcarver.XStreamStateCarver.loadState(760, 0, "scribe-java_freqT")
//Note that we are using a 'response' object which was tested as part of unit test elsewhere.
@gdevanla
gdevanla / partition.groovy
Last active August 29, 2015 13:57
Clojure like partition-by function for groovy
//Accepts a closure that takes one argument. The closure should return true/false.
//The closure is the predicate which will be checked to create new partitions
def partition(coll, Closure cond) {
def x = coll.drop(1).inject([[coll[0]]]) { acc, it ->
if ( cond(it) ) {
acc << [it]
}
else
{
@gdevanla
gdevanla / y-combinator-clojure.clj
Last active October 9, 2022 16:46
Y-Combinator in Clojure based on Jim Weirich's talk Y-NOT
;; This gist roughly transribes the demo
;; by Jim Weirich during his talk on Y-Combinator
;; called Y-Not.
;; http://www.infoq.com/presentations/Y-Combinator
;; Jim does a phenomenal job of explaining in the demo.
;; Therefore, this gist only attempts to provide
;; the code example from the poor quality video
;; The examples are simplified at some places
;; based on how I tried to understand it
//Cartesian product of input (for Integers)
//similar to itertools.product in Python
def l = [ [1,2] , [3,4] , [5, 6]]
def product(l){
def p = { l1, l2 ->
l1.inject([]) { acc, i ->
acc + l2.inject([]) { acc1, j ->
if ( i instanceof List) { acc1 << i + j}
// does a product on two lists
h = [ 1: "a", 2:"b", 3:"c"]
j = [ 1: ["x", "p"], 2: ["z"], 3:["y"]]
h.entrySet().inject([]) { acc, h1 ->
acc + j.get(h1.key).inject([]) { acc1, it-> acc1 << [h1.value, it]} }
;;visit all nodes using reduce
;;clojure
;;store nodes in visited order.
(defn visit [g]
(let [walk (fn walk [g seen n]
(reduce (fn [seen, m]
(cond
(seen m) seen
:else (walk g (conj seen m) m) )) seen (g n))
;;Computes all possible paths through a graph (DAG)
;;assumes the graph is a DAG
;; Clojure
(defn all-paths [g]
(let [walk (fn walk [g seen n]
(cond
(empty? (g n)) seen
:else (mapcat (fn [m]
(walk g (conj seen m) m)) (g n))))]
;; Counting Coins
;; problem from "Coding for Interviews" news letter.
;; notes: switching to vector from raw list gave a tremendous performance boost
;; uses memoize as variant of Y-combinator.
(let
[coins (map read-string (clojure.string/split (read-line) #","))
amt (read-string (read-line))
change3 (fn [rec amt coins]
(cond