Skip to content

Instantly share code, notes, and snippets.

View AlBaker's full-sized avatar

Al Baker AlBaker

View GitHub Profile
@leifericf
leifericf / bb.edn
Last active January 9, 2024 20:21
Consuming 3rd Party APIs Using Clojure (Babashka)
{:paths ["src"]
:deps {org.babashka/json {:mvn/version "0.1.1"}}}
@kstrempel
kstrempel / gist:3b2f707c2f9019e55f4721b3a80cfbf2
Last active January 27, 2021 16:01
Running OpenAI with in Clojure and libpython-clj
(ns pyclojure.core
(:gen-class)
(:require [libpython-clj.require :refer [require-python]]
[libpython-clj.python :as py]))
; import gym
; env = gym.make('CartPole-v0')
; for i_episode in range(20):
; observation = env.reset()
; for t in range(100):
@pjstadig
pjstadig / transducers.md
Last active June 8, 2021 13:22
The secret feature of transducers that no one talks about!

The Pledge

One thing that always made me a little sad about transducers was how map lost its ability to iterate multiple collections in parallel. This is actually my favorite feature of map. For example:

(map + (range 5) (range 5 10))
=> (5 7 9 11 13)

One somewhat practical use of this is if you want to compare two sequences, pairwise, using a comparator. Though I wish that every? took multiple collections, this is an adequate substitute:

@evren
evren / PrintProtobufLogs.java
Created February 17, 2017 01:29
Example showing how to read Stardog's audit log
/*
* Copyright (c) 2017 Stardog Union. <http://www.stardog.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@aphyr
aphyr / with-retry.clj
Created January 20, 2016 03:14
Recur from within catch block
(defrecord Retry [bindings])
(defmacro with-retry
"It's really fucking inconvenient not being able to recur from within (catch)
expressions. This macro wraps its body in a (loop [bindings] (try ...)).
Provides a (retry & new bindings) form which is usable within (catch) blocks:
when this form is returned by the body, the body will be retried with the new
bindings."
[initial-bindings & body]
(assert (vector? initial-bindings))
@cgrand
cgrand / transmogrify.clj
Last active August 29, 2015 14:18
transmogrify->> rewrites last-threaded forms to use transducers.
(defmulti transmogrify
"Rewrites the last form of a thread-last to use transducer (if possible)."
(fn [f xform src & args] f))
(defmacro transmogrify->>
"Like ->> but uses transducers"
([x] x)
([src & xs]
(let [end (last xs)
xforms (butlast xs)
@favila
favila / async-util.clj
Created November 28, 2014 17:06
Some missing pieces of core.async. as-transducer: Make a transducer function easily without Clojure 1.7. go-pipe: async/pipe, but returns the go-loop. fast-pipeline-blocking: faster than async/pipeline-blocking, but unordered results. blocking-consumer: consume a channel with multiple worker threads.
(ns favila.async-util
"Some missing pieces of core.async.
as-transducer: Make a transducer function easily without Clojure 1.7.
go-pipe: async/pipe, but returns the go-loop.
fast-pipeline-blocking: faster than async/pipeline-blocking, but unordered results.
blocking-consumer: consume a channel with multiple worker threads."
(:require [clojure.core.async :as async
:refer [go go-loop <! >! <!! >!! close! take! put! chan]]))
@gigasquid
gigasquid / fizzbuzz.clj
Created November 7, 2014 23:36
FizzBuzz without Conditionals
(defn fizzbuzz [n]
(let [all-nums (range 0 n)
folder (fn [fb-str p-num fb-coll]
(mapcat (fn [x] (cons fb-str (rest x)))
(partition-all p-num fb-coll)))
fizz (folder "fizz" 3 all-nums)
buzz (folder "buzz" 5 fizz)
fizzbuzz (folder "fizzbuzz" 15 buzz)]
fizzbuzz))
@kachayev
kachayev / css-parser.md
Last active November 12, 2022 04:20
Parsing CSS file with monadic parser in Clojure
if (arguments.length != 2) {
print("Usage: jjs -cp lib/kvclient.jar oracle-nosql-put.js -- <key> <value>");
exit(1);
}
var oracle = Packages.oracle;
var KVStore = oracle.kv.KVStore;
var KVStoreConfig = oracle.kv.KVStoreConfig;
var KVStoreFactory = oracle.kv.KVStoreFactory;