Skip to content

Instantly share code, notes, and snippets.

View d0minicw0ng's full-sized avatar

Dominic Wong d0minicw0ng

View GitHub Profile
@d0minicw0ng
d0minicw0ng / recursions.hs
Last active August 29, 2015 14:04
Recusions in Haskell
maximum' :: (Ord a) => [a] -> a
maximum' [] = error "maximum of empty list"
maximum' [x] = x
maximum' (x:xs)
| x > maxTail = x
| otherwise = maxTail
where maxTail = maximum' xs
-- using max
-- maximum' (x:xs) = max x (maximum' xs)
@d0minicw0ng
d0minicw0ng / jquery.onvisible.js
Last active July 7, 2016 01:01
jquery.onvisible.js
/**
* function $.fn.onVisible runs callback function once the specified element is visible.
* callback: A function to execute at the time when the element is visible.
* example: $(selector).onVisible(callback);
*/
(function($) {
$.fn.onVisible = function (callback) {
var self = this;
@d0minicw0ng
d0minicw0ng / filter_destroyed.js.coffee
Created October 24, 2014 10:09
Knockout.js custom foreach binding that filters objects that have been destroyed. (The rule can be customized or possibly passed in)
ko.bindingHandlers.filterDestroyed =
init: (element, valueAccessor, allBindings, viewModel, bindingContext) ->
filteredValueAccessor = ->
observableArray = valueAccessor()
_.filter observableArray(), (viewModel) ->
viewModel._destroy() is false
ko.bindingHandlers["template"]["init"] element, ko.bindingHandlers["foreach"].makeTemplateValueAccessor(filteredValueAccessor)
update: (element, valueAccessor, allBindings, viewModel, bindingContext) ->
/*
Usage:
myView.setOnTouchListener(new OnSwipeTouchListener(this) {
@Override
public void onSwipeDown() {
Toast.makeText(MainActivity.this, "Down", Toast.LENGTH_SHORT).show();
}
}
@d0minicw0ng
d0minicw0ng / OnDoubleTapListener.java
Created December 21, 2014 09:56
Android DoubleTap Listener from codepath Android bootcamp
/*
Usage:
myView.setOnTouchListener(new OnDoubleTapListener(this) {
@Override
public void onDoubleTap(MotionEvent e) {
Toast.makeText(MainActivity.this, "Double Tap", Toast.LENGTH_SHORT).show();
}
});
@d0minicw0ng
d0minicw0ng / rename.sh
Created January 19, 2015 06:38
Shell script for global renaming
# 1. Recursively find all files in the directory with the extension ".js.cofee" and feeds the files into an input stream
# 2. Run `sed` to perform a string replace ('hello' => 'world') and create backup files
# 3. Find all the backup files and remove them recursively from the directory
find . -name "*.js.coffee" | while read a
do
sed -iBACKUP 's/hello/world/' $a
done
find . -name "*BACKUP" -delete
@d0minicw0ng
d0minicw0ng / lazy.js.coffee
Created January 24, 2015 05:25
Knockout Lazy ViewModel
class OPL.ViewModels.Lazy extends OPL.ViewModels.Base
###
lazyFlag is a string that holds the flag's name.
When the flag changes from false to true, it creates
the lazy objects and methods. When it changes from true
to false, it destroys the lazy objects and methods.
###
lazyFlag: "editMode"
constructor: ->
@d0minicw0ng
d0minicw0ng / 64_bit_compatible.sh
Created January 29, 2015 11:54
Check if your Macbook is 64bit compatible
# It returns a value called hw.cpu64bit_capable which is either 0 (not 64 bit compatible) or 1 (64 bit compatible)
sysctl hw | grep 64bit
@d0minicw0ng
d0minicw0ng / mirror.sh
Created February 5, 2015 12:39
It is used to download a website.
# Explanation of the various flags:
# --mirror – Makes (among other things) the download recursive.
# --convert-links – convert all the links (also to stuff like CSS stylesheets) to relative, so it will be suitable for offline viewing.
# --adjust-extension – Adds suitable extensions to filenames (html or css) depending on their content-type.
# --page-requisites – Download things like CSS style-sheets and images required to properly display the page offline.
# --no-parent – When recursing do not ascend to the parent directory. It useful for restricting the download to only a portion of the site.
wget --mirror --convert-links --adjust-extension --page-requisites --no-parent http://example.org
@d0minicw0ng
d0minicw0ng / js_to_coffee.vim
Created April 17, 2015 02:36
Copy JavaScript and paste CoffeeScript
" Grab pbpaste clipboard contents pipe them through js2coffe and paste it
" in the current buffer
function! PasteAsCoffee()
:read !pbpaste | js2coffee
endfunction
command! PasteAsCoffee :call PasteAsCoffee()