Skip to content

Instantly share code, notes, and snippets.

View bepitulaz's full-sized avatar
🏗️
Start doing indie hacking again!

Asep Bagja Priandana bepitulaz

🏗️
Start doing indie hacking again!
View GitHub Profile
@bepitulaz
bepitulaz / lib.rs
Created August 17, 2021 16:54
Calculating simple moving average in Rust and target Node native module.
use node_bindgen::derive::node_bindgen;
#[node_bindgen]
pub fn simple_moving_average(array_prices: Vec<f64>, window: f64) -> Vec<f64> {
let interval = window as usize;
let mut index = interval - 1;
let length = array_prices.len() + 1;
let mut results = Vec::new();
@bepitulaz
bepitulaz / lib.rs
Created August 17, 2021 16:50
Calculate simple moving average in Rust and targeting WASM.
use wasm_bindgen::prelude::*;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
extern "C" {
@bepitulaz
bepitulaz / index.js
Created August 17, 2021 16:11
Calculate simple moving average in JavaScript.
/**
* Calculate the simple moving average from stock prices.
* @param {Array} prices - The list of prices.
* @param {number} interval - The number of periods to calculate.
* @return {Array} The list of SMA value.
*/
exports.simpleMovingAverage = (prices, interval) => {
let index = interval - 1;
const length = prices.length + 1;
let results = [];
@bepitulaz
bepitulaz / main.rs
Created August 14, 2021 15:34
Spawning 1000 threads to do concurrent HTTP call with Rust standard library.
// This gist is a solution from a challenge that I found in Twitter.
// The challenge is writing code to do 1000 HTTP GET concurently with any favourite programming languages.
// Here's the URL: https://twitter.com/codingfess/status/1426142985532575747
use std::thread;
use std::io::prelude::*;
use std::net::{TcpStream};
fn http_call(hostname: &str) {
let mut stream = TcpStream::connect((hostname, 80)).unwrap();
let header = format!("GET /get HTTP/1.1\r\nHost: {}\r\nAccept: */*\r\nConnection: close\r\n\r\n", hostname);
@bepitulaz
bepitulaz / soal-ramadan.js
Created May 30, 2017 08:58
Quiz Ramadan dari Froyo Framework.
const express = require('express');
const app = express();
let personData = [
{
name: 'Budi',
age: '42',
origin: 'Jawa Barat'
},
{
@bepitulaz
bepitulaz / blinktanpadelay.ino
Created April 28, 2017 08:27
Multitasking Arduino dengan menggunakan millis
const int ledPin = 13; // pin yang digunakan untuk LED
// variabel yang akan selalu berganti
int ledState = LOW; // ledState digunakan untuk mengubah kondisi LED
long previousMillis = 0; // akan menyimpan milidetik terakhir dari loop
// inilah variabel pengganti delay(1000)
long interval = 1000; // interval blink dalam ms
void setup() {
@bepitulaz
bepitulaz / panner.js
Created April 7, 2017 07:57
Belajar membuat 3D sound dengan menggunakan Panner Node
(function() {
let audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// Create the oscillator
let osc1 = audioCtx.createOscillator();
// Create the gain node
let gainOsc1 = audioCtx.createGain();
// Create the panner
@bepitulaz
bepitulaz / oscillator.js
Created April 5, 2017 05:27
Membuat suara dengan menggunakan Web Audio API
(function() {
let audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// Create the oscillator
let osc1 = audioCtx.createOscillator();
let osc2 = audioCtx.createOscillator();
// Create the gain node
let gainOsc1 = audioCtx.createGain();
let gainOsc2 = audioCtx.createGain();
@bepitulaz
bepitulaz / blink.ino
Created March 27, 2017 09:34
Menyalakan LED Pada ESP8266
@bepitulaz
bepitulaz / kode-diuji.js
Last active February 22, 2017 05:09
Sample of unit testing in my article.
/**
* Fungsi ini digunakan untuk menghitung harga barang setelah diskon.
* @param {Number} discountRate - nilai diskon yang diberikan
* @param {Number} originalPrice - harga barang sebelum diskon dalam Rupiah
* @returns {Number}
*/
function calculateDiscount(originalPrice, discountRate) {
let discount = originalPrice * discountRate / 100;
return originalPrice - discount;
}