Skip to content

Instantly share code, notes, and snippets.

Avatar

Aleksandr Zhuravlёv zelark

View GitHub Profile
@zelark
zelark / take-first-sorted-by.clj
Created Nov 26, 2020 — forked from Ivana-/take-first-sorted-by.clj
Lazysecs efficient reducer, which returns n first elements of sequence, sorted by keyfn & comp
View take-first-sorted-by.clj
(defn take-first-sorted-by
"Performs the same result as (take n (sort-by keyfn comp coll))
but more efficient in terms of time and memory"
([n keyfn coll] (take-first-sorted-by n keyfn compare coll))
([n keyfn ^java.util.Comparator comp coll]
(if (pos? n)
(let [m ^java.util.TreeMap (java.util.TreeMap. comp)
m-size (volatile! 0)
;; if it is guaranteed that (keyfn v) will be unique for all v in coll
;; we can attach :unique? key to keyfn meta and algorythm will use single val map
@zelark
zelark / torrent-viewer.clj
Last active Nov 11, 2020
babashka script for viewing torrents files
View torrent-viewer.clj
#!/usr/bin/env bb
(require '[clojure.java.io :as io])
(require '[bencode.core :refer [read-bencode]])
(require '[clojure.walk :refer [prewalk]])
(require '[clojure.pprint :refer [pprint]])
(import 'java.io.PushbackInputStream)
(defn bytes->strings [coll]
(prewalk #(if (bytes? %) (String. % "UTF-8") %) coll))
View fizzbuzz.clj
(defn fizzbuzz [n]
(let [fizzes (cycle ["" "" "Fizz"])
buzzes (cycle ["" "" "" "" "Buzz"])
pattern (map str fizzes buzzes)
numbers (map str (rest (range)))]
(take n (map #(some not-empty %&) pattern numbers)))) ;; another option would be `(partial max-key count)`
(run! println (fizzbuzz 100))
View lessons-learned.md

Lessons learned; The nice and accurate counsel of Alex Miller, programmer

TL:DR

  • Be intentional
  • Stay connected to the problem
  • Use tables to explore alternatives
  • Make a picture

1. Read the docs!

View lazyness.clj
(defn eager-map [f coll]
(when-first [x coll]
(println "iteration")
(cons (f x)
(eager-map f (rest coll)))))
(defn lazy-map [f coll]
(lazy-seq
(when-first [x coll]
(println "iteration")
@zelark
zelark / hashmap.c
Last active Apr 5, 2020
You can find the original example here https://youtu.be/wg8hZxMRwcw
View hashmap.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASHMAP_SIZE 16
typedef struct entry_t {
char *key;
char *value;
struct entry_t *next;
View hashmap.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 16
typedef struct entry_t {
char *key;
char *value;
struct entry_t *next;
@zelark
zelark / hashmap.c
Created Apr 5, 2020
You can find the original example herehttps://www.youtube.com/watch?v=wg8hZxMRwcw
View hashmap.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 16
typedef struct entry_t {
char *key;
char *value;
struct entry_t *next;
@zelark
zelark / lispy.c
Last active Apr 2, 2020
Build your own LISP
View lispy.c
#include "mpc.h"
#include <editline/readline.h>
#define LASSERT(args, cond, fmt, ...) \
if (!(cond)) { \
lval* err = lval_err(fmt, ##__VA_ARGS__); \
lval_del(args); \
return err; \
}
You can’t perform that action at this time.