Skip to content

Instantly share code, notes, and snippets.

View ampersanda's full-sized avatar
❤️
.clj

Lucky Pradana ampersanda

❤️
.clj
View GitHub Profile
@ampersanda
ampersanda / Implementation.dart
Created September 24, 2019 04:27
Sticky Sliver Header
build () {
SliverPersistentHeader(
delegate: SliverAppBarDelegate((context, _, __) {
return Container(
color: CupertinoColors.white,
child: Padding(
padding: EdgeInsets.symmetric(
vertical: 8.0, horizontal: 16.0),
child: SearchView(
autofocus: true,
; When a candle finishes burning it leaves a leftover. makeNew leftovers can be combined to make a new candle, which, when burning down, will in turn leave another leftover.
; You have candlesNumber candles in your possession. What's the total number of candles you can burn, assuming that you create new candles as soon as you have enough leftovers?
; Example
; For candlesNumber = 5 and makeNew = 2, the output should be
; candles(candlesNumber, makeNew) = 9.
; Here is what you can do to burn 9 candles:
(defn expand-home [s]
(if (.startsWith s "~")
(clojure.string/replace-first s "~" (System/getProperty "user.home"))
s))
@ampersanda
ampersanda / is_file_exists.clj
Created October 28, 2019 23:31
Is file exists?
(defn file-exists? [_]
(.exists (clojure.java.io/as-file _)))
@ampersanda
ampersanda / read_password_cli.clj
Created October 28, 2019 23:33
CLI - read password
;; password input is visible when use run
;; but it's invincible if it's executed using java -jar target/your-uberjar-standalone.jar
(defn read-password [prompt]
;; Based on https://groups.google.com/forum/#!topic/clojure/ymDZj7T35x4
(if (= "user" (str (.getName *ns*)))
(do
(print (format "%s [will be echoed to the screen]" prompt))
(flush)
(read-line))
(defn write-file [filename spitted-string]
(clojure.java.io/make-parents filename)
(spit filename spitted-string))
@ampersanda
ampersanda / candles.clj
Created November 9, 2019 04:42
Code Signal - Candles
(defn candles[c n]{:pre[(not(nil? c))(>= c 1)(<= c 15)(not (nil? n))(>= n 2)(<= n 5)]}(if(> n c)c(loop [r c b [c]](let[m (mod r n)f(int(double(/ r n)))](if(<= r n)(reduce + (flatten[b f]))(recur(+ m f)(conj b f)))))))
@ampersanda
ampersanda / decipher.clj
Created November 9, 2019 04:43
Code Signal - Decipher
(def decipher
(fn [e]
(loop [s e
r []]
(if (= (count s) 0)
(apply str (map #(char (read-string (apply str %)))r))
(let [l (if (= \1 (first s)) 3 2)
v (vec s)]
(recur (subvec v l)
(conj r (take l v))))))))
@ampersanda
ampersanda / debounce.dart
Last active November 28, 2019 04:22
TextField Input Debouncing
// declare variable at the top inside class
Timer _debounce;
Duration _debounceDuration = Duration(seconds: 1);
// initState
@override
void initState() {
super.initState();
// add listener to textfield controller
@ampersanda
ampersanda / lazyload.js
Created December 1, 2019 14:30
Simple and Modern LazyLoad
// https://twitter.com/samccone/status/1200967148580921344?s=20
this.obs = new IntersectionObserver(onHit, {
rootMargin: '0px',
threshold: 0.2
});
this.obs.observe(elms);
onHit(entries) {