Skip to content

Instantly share code, notes, and snippets.

View Ke-'s full-sized avatar

Ke Ke-

  • Zeroloop Limited
  • New Zealand
View GitHub Profile
@Ke-
Ke- / gist:54b5ba95b9490070e7a6
Last active August 29, 2015 14:22
pop type for Lasso 9 — converted from a L8.x type floating around.
<?lasso
define pop => type {
data
public pop_net = null,
public pop_mode = '',
public pop_timeout = 15,
public pop_token = '',
public pop_index = 0,
public pop_err = array,
@Ke-
Ke- / Byte example
Last active August 29, 2015 14:13
Byte example
local(data) = bytes('Hello')
my_ds->sql("
DROP TABLE IF EXISTS bytes_example;
CREATE TABLE bytes_example (
`data` binary(16) NOT NULL
);
INSERT INTO bytes_example(data) VALUES (X'" +#data->encodehex + "');
// Cache method result on a thread level
define whatmethod => {
return givenblock->methodname
}
define whattype => {
return givenblock->self->type
}
@Ke-
Ke- / Short string integers
Created January 7, 2015 06:46
integer->encodeBase64Short, string->decodeBase64Short
/*
1: AQ
62: Pg
100: Z
1000: 6AM
10000: ECc
100000: oIYB
1000000: QEIP
10000000: gJaY
1000000000: AMqaOw
@Ke-
Ke- / cache_method.lasso
Last active May 9, 2016 22:13
Used either for a singleton effect or just for performance.
// Cache method result on a thread level
define cache_method(c::capture = givenblock) => {
#c->isnota(::capture) ? return #c
local(v) = #c->self ? #c->self->type->asstring + '.' + #c->methodname | #c->methodname
var(#v)->isa(::null) ? var(#v) = #c->detach & invoke
return var(#v)
}
@Ke-
Ke- / gist:13a4ac6efbd3c3bc1d61
Last active August 29, 2015 14:08
Log slow requests
define sys_slow_value => 1000.0
define sys_slow_output(p=null) => {
local(s) = micros
define_atend({
local(t) = (micros - #s) / 1000.0
#t > sys_slow_value && web_request ? log_critical('SLOW: ' + client_url + ' — ' + #t + ' milliseconds')
})
}
@Ke-
Ke- / gist:aaee6ee0dff54f14fc9e
Last active August 29, 2015 14:07
Active Statement Example
local(
query = .ds->table(
::mytable
)->select(
#columns
)->where(
#where
)->orderby(
#orderby
)->limit(
define sys_info => (
'sys_getthreadcount: ' + sys_getthreadcount +'\n'
'sys_getheapsize: ' + sys_getheapsize +'\n'
'sys_getheapfreebytes: ' + sys_getheapfreebytes +'\n'
'sys_getbytessincegc: ' + sys_getbytessincegc +'\n'
'sys_time: ' + sys_time '\n'
'sys_getegid: ' + sys_getegid '\n'
'sys_geteuid: ' + sys_geteuid '\n'
'sys_getlogin: ' + sys_getlogin '\n'
'sys_getpid: ' + sys_getpid '\n'
@Ke-
Ke- / Hex to RGB
Last active August 29, 2015 14:04
define char2int(p::string,mult::integer=1) => {
match(#p) => {
case('a')
return 10 * #mult
case('b')
return 11 * #mult
case('c')
return 12 * #mult
case('d')
return 13 * #mult
// Inject construct similar to Ruby's inject.
define trait_foreach->inject(p::any) => {
local(gb) = givenblock
#gb->detach
.foreach => {
#p = #gb(#p,#1) || #p
}
return #p
}