Skip to content

Instantly share code, notes, and snippets.

@JaHIY
JaHIY / with.tcl
Last active May 4, 2016 06:46
Tcl: `with` proc from `with-open` in clojure
#!/usr/local/opt/tcl-tk/bin/tclsh
package require Tcl 8.6
proc with {args} {
set lengthOfArgs [llength $args]
if {($lengthOfArgs % 2) == 0} {
return -code error {wrong # args: should be "with ?varName1 channelId1 varName2 channelId2 ...? command"}
}
if {$lengthOfArgs == 1} {
@JaHIY
JaHIY / flatten.pl
Last active December 31, 2015 04:20
flatten list in perl
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.014;
use Data::Dumper;
sub flatten (+) {
@JaHIY
JaHIY / morse_decode.p6
Last active December 23, 2015 04:01
Morse code translator in 1 line of perl6.
my %h = <.--.-. @ ...-..- $ .-..-. " ..--.- _ -....- - .-.-. + -...- = -.-.-. ; ---... : ----- 0 ----. 9 ---.. 8 --... 7 -.... 6 ..... 5 ....- 4 ...-- 3 ..--- 2 .---- 1 .--.-. @ -..-. / ..--.. ? --..-- , .-.-.- . --.. Z -.-- Y -..- X .-- W ...- V ..- U - T ... S .-. R --.- Q .--. P --- O -. N -- M .-.. L -.- K .--- J .. I .... H --. G ..-. F . E -.. D -.-. C -... B .- A>; for lines() { say .split(/<space>/).map({ %h{$_} // $_ }).join }
@JaHIY
JaHIY / check_params.pl
Last active September 16, 2015 13:53
wrap compile function in Type::Params
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.010;
use Data::Dumper;
use Type::Params qw(compile);
use Types::Standard -types;
@JaHIY
JaHIY / decode_base64.c
Last active September 11, 2015 04:41
base64 in C language
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
int main(void) {
const uint8_t base64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
uint32_t buf = 0;
int8_t iter = 0;
bool enter_loop = true;
@JaHIY
JaHIY / strings.sh
Last active November 26, 2021 00:33
String-related functions in POSIX shell
#!/bin/sh -
substr() {
awk -v s="$1" -v i="$2" -v l="$3" 'BEGIN { print substr(s, i+1, l); }'
}
index() {
awk -v s="$1" -v c="$2" 'BEGIN { print index(s, c) - 1; }'
}
@JaHIY
JaHIY / operators.sh
Last active September 12, 2015 04:16
Bitwise operators in POSIX shell
#!/bin/sh -
calc() {
printf '%s\n' \
'define bit_and (x, y) {
auto min, max, d, i, ret
i = 1
ret = 0
if (x > y) {
min = y
@JaHIY
JaHIY / spawn.pl
Last active August 29, 2015 14:27
interprocess communication in perl
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.014;
use experimental qw/smartmatch/;
use Carp qw/croak/;
use Data::Dumper;
@JaHIY
JaHIY / yanwenzi.json
Created May 15, 2015 06:45
A岛颜文字
{
"hacfun":[
"|∀゚",
"(´゚Д゚`)",
"(;´Д`)",
"(`・ω・)",
"(=゚ω゚)=",
"| ω・´)",
"|-` )",
"|д` )",
@JaHIY
JaHIY / perl2pkg.pl
Created April 4, 2015 16:19
convert perl module to PKGBUILD through metacpan API
#!/usr/bin/env perl
use strict;
use warnings;
use utf8::all;
use Carp;
use HTTP::Tiny;
use JSON qw(decode_json);
use Template;