Skip to content

Instantly share code, notes, and snippets.

View rikyperdana's full-sized avatar
💭
Alhamdulillah

Riky Perdana rikyperdana

💭
Alhamdulillah
View GitHub Profile
@rikyperdana
rikyperdana / matrix.js
Last active April 19, 2024 02:36
Matrix Operations in JS
withAs = (obj, cb) => cb(obj)
sum = arr => arr.reduce((a, b) => a + b)
mul = arr => arr.reduce((a, b) => a * b)
sub = arr => arr.splice(1).reduce((a, b) => a - b, arr[0])
deepClone = obj => JSON.parse(JSON.stringify(obj))
shifter = (arr, step) => [
...arr.splice(step),
...arr.splice(arr.length - step)
]
@rikyperdana
rikyperdana / fp.py
Last active August 26, 2024 04:23
Functional Python
# Array Methods
array = lambda n, func=(lambda x: x) : map([*range(n)], func)
filter = lambda arr, func: [i for i in arr if func(i)]
find = lambda arr, func: filter(arr, func)[0]
map = lambda arr, func: [func(i) for i in arr]
def reduce(arr, init, func):
for i in arr: init = func(init, i)
return init
# Math Methods
@rikyperdana
rikyperdana / statistics.js
Last active September 25, 2021 03:46
Statistics in JS with Functional Paradigm
var
withThis = (obj, cb) => cb(obj),
get = prop => obj => obj[prop],
add = array => array.reduce((acc, inc) => acc + inc),
sub = array => array.reduce((acc, inc) => acc - inc),
mul = array => array.reduce((acc, inc) => acc * inc),
pow = asc => num => Math.pow(num, asc),
range = array => Math.max(...array) - Math.min(...array),
between = (low, middle, high) =>
(low <= middle) && (middle <= high),
@rikyperdana
rikyperdana / autoform-introduction.md
Created June 27, 2020 00:11
How I built Automatic Form Generator based on Mithril

How I built Automatic Form Generator based on Mithril

I still remember the days when I consistently glorify Meteor JS for all of my projects and event wrote a dedicated medium post titled 'Why I chose Meteor JS'. There was never a day since then I haven't thanked MeteorJS for all goodness it brought that delivers me to become a bit matured js developer. It lifted all the burden of steep learning curves of other frameworks and kept me away from the so called Javascript Fatigueness all around. And one of the biggest credit of my achievements has to go to aldeed:autoform that allows me and other MeteorJS developer to easily build forms in the simplest manner of writing a schema. I wouldn't dare to imagine how troublesome and terrible the tasks would be without it as my projects are mostly consists of deeply nested forms for database management and hospital information.

m.render(document.body, m('table', m('thead', m('tr', m('th', 'Kolom1'))), m('tbody', m('tr', m('td', 'baris1')))));
@rikyperdana
rikyperdana / neural-data-normalizer.ls
Last active June 7, 2019 15:46
Re-engineered version of adadgio:neural-data-normalizer
# Neural Data Normalizer by utilizing recursion
# Works well for Synaptic JS
# But require list of strings to be defined beforehand
# Written in LiveScript and with lodash methods
obj = name: \workout, duration: 120, tags: <[gym weights]>
opts =
<[sleep lunch workout]>
<[romance bed wine salad weights gym]>
@rikyperdana
rikyperdana / aggregate.ls
Last active January 13, 2019 15:48
Play with LiveScript and Mongo Aggregate
/*
lsc -wc aggregate.ls
mongo localhost:27017/dbname
*/
# Daftar antrian bayar pasien
printjson (._firstBatch) db.pasien.aggregate pipe =
a = $match: rawat: $elemMatch: billRegis: $ne: true
b = $unwind: \$rawat
c = $project:
@rikyperdana
rikyperdana / functional_matrix.ls
Created October 30, 2018 04:45
Matrix Operation with Functional Programming
sum = -> it.reduce (res, inc) -> res + inc
transpose = (arr) -> [to arr.0.length-1]map (i) ->
[to arr.length-1]map (j) -> arr[j][i]
mathMul = -> it.reduce (a, b) -> if a.0.length is b.length
[to a.length-1]map (i) -> [to b.0.length-1]map (j) ->
sum a[i]map (k, l) -> k * b[l][j]
@rikyperdana
rikyperdana / statistics.ls
Last active November 13, 2018 01:08
Functional Statistics
sum = -> it.reduce (res, inc) -> res + inc
multi = -> it.reduce (res, inc) -> res * inc
divs = -> it.reduce (res, inc) -> res / inc
mean = -> sum(it)/it.length
diff = -> if mean it then it.map -> it - that
squared = -> it.map -> Math.pow it, 2
pows = (num, arr) -> arr.map -> Math.pow it, num
min = -> (.0) [...it]sort!
max = -> (.0) [...it]sort!reverse!
range = -> max(it) - min(it)
@rikyperdana
rikyperdana / funConds
Created May 26, 2018 01:31
Functional alternative of 'if else' conditional
Functional programming in javascript had me captured since it was popular a few years back.
I started to ditch for loops with map/find/filter/reduce, using short-circuit instead of conditional or
ternary operator, and many migrations to immutability mindset. I have less variable naming to write on and think about, since
I only need to pass values from one function to another, wheter it be a named one or not.
But there's some kind of codes that still bothers me. Here's a dummy example:
```
if(1 + 1 === 2){
console.log('addition');
} else if (2 * 3 === 6){