Skip to content

Instantly share code, notes, and snippets.

View nilz3ro's full-sized avatar
🧃

nil nilz3ro

🧃
View GitHub Profile
@nilz3ro
nilz3ro / iso.countries.js
Last active November 18, 2023 05:38
Write json file
// Paste this in a node v4.x REPL.
// let iso = require('./iso.countries.js');
// iso.writeCountryJsonFile('test.js', (country) => { let o={}; o['name'] = country['name']; o['ISO3166-1-Alpha-2'] = country['ISO3166-1-Alpha-2']; o['code'] = country['ISO3166-1-Alpha-2']; o['Dial'] = country['Dial']; return o;});
'use strict';
let fs = require('fs');
let countries;
const writeCountryJsonFile = (outputFilename, mapFunction) => {
@nilz3ro
nilz3ro / sublist.rs
Created January 21, 2022 21:45
Trait example
#[derive(Debug, PartialEq)]
pub enum Comparison {
Equal,
Sublist,
Superlist,
Unequal,
}
trait IncludedIn {
fn included_in(&self, rhs: Self) -> bool;
@nilz3ro
nilz3ro / ownership.md
Last active April 30, 2021 17:26
Rust's Ownership principles.

Rust's Ownership Principles

Ownership

Ownership is is a core concept in Rust and it provides a way for Rust and its programmers to deal with memory management and reference validation. Each value in Rust has an Owner which is basically the scope that the value belongs to. This could be a function or block scope or a struct etc.

Move

When you pass a value to a function (and in other scenarios that I will skip over for brevity)

@nilz3ro
nilz3ro / reverse.js
Last active May 29, 2019 15:31
es6, recursion, rest and spread operators, destructuring and default parameter value.
'use strict';
const reverse = (enumerable, sorted=[]) => {
let [head, ...tail] = enumerable;
return head === undefined
? sorted.join('')
: reverse(tail, [head, ...sorted])
;
}

Keybase proof

I hereby claim:

  • I am nilz3ro on github.
  • I am nilz3ro (https://keybase.io/nilz3ro) on keybase.
  • I have a public key ASDkhNqKgtaGgI5RxhmNLg9QPj5RxJPPObkfsxPUXBP7ywo

To claim this, I am signing this object:

@nilz3ro
nilz3ro / darken-by-percentage.js
Last active November 8, 2017 16:52
Darken Hex Colors by percentage as a JS one-liner 😎
import { chunk } from 'lodash';
const darkenByPercentage = (hexString, percent) =>
chunk(hexString.substr(1).split(''), 2)
.reduce((memo, pair) => [...memo, `0x${pair.join('')}`], '')
.map(Number)
.map(digit => digit - Math.ceil(digit * (percent / 100)))
.reduce((memo, digit) => `${memo}${digit.toString(16)}`, '#');
// darkenByPercentage('#ffffff', 20);
@nilz3ro
nilz3ro / string-reverse.js
Created February 15, 2017 15:27
Recursion Example
// One liner.
const reverseIt = (string, memo='') => string.length ? reverseIt(string.substr(1), string.substr(0,1) + memo) : memo
// same as:
function reverseIt2 (string, memo='') {
if (string.length) {
return reverseIt2(string.substr(1), string.substr(0,1) + memo);
} else {
return memo;

Keybase proof

I hereby claim:

  • I am nilz3ro on github.
  • I am nilz3ro (https://keybase.io/nilz3ro) on keybase.
  • I have a public key ASCVf434N2WZYWMe6-LVI79LBo6zu5LjyajFOnuSYiwLdAo

To claim this, I am signing this object:

@nilz3ro
nilz3ro / init.vim
Last active August 21, 2017 15:05
(n)Vim Configuration
call plug#begin()
Plug 'vim-airline/vim-airline'
Plug 'tpope/vim-sensible'
Plug 'scrooloose/nerdtree'
call plug#end()
@nilz3ro
nilz3ro / commit_commenter.rb
Last active June 5, 2017 19:20
Add the latest commit hash to a given file, as an html comment.
require 'fileutils'
class CommitCommenter
def initialize(git_project_path)
@project_path = git_project_path.chomp
end
def latest_commit_hash
FileUtils.cd(@project_path)
return `git rev-parse HEAD`.chomp