Skip to content

Instantly share code, notes, and snippets.

View Hugoberry's full-sized avatar

Igor Cotruta Hugoberry

View GitHub Profile
@Hugoberry
Hugoberry / ReversingBits.ps1
Last active January 24, 2017 16:37
An example of reversing bits in a 32-bit number
function reverse(){
param([Int32]$b)
$b = (($b -band 0xf0) -shr 4) -bor (($b -band 0x0f) -shl 4)
$b = (($b -band 0xcc) -shr 2) -bor (($b -band 0x33) -shl 2)
$b = (($b -band 0xaa) -shr 1) -bor (($b -band 0x55) -shl 1)
$b
}
# on a 64bit machine this is an alternative to reverse() function
function reverse64(){
param([Int32]$x)
@Hugoberry
Hugoberry / Fisher-Yates-shuffle.m
Last active February 1, 2017 18:11
The Fisher–Yates shuffle is an algorithm for generating a random permutation of a finite set—in plain terms, the algorithm shuffles the set. The algorithm effectively puts all the elements into a hat; it continually determines the next element by randomly drawing an element from the hat until no elements remain. The algorithm produces an unbiase…
(n) =>
let
swapAnyOrder = (_,from,to)=> if from = to then _ else
(if from < to then swap(_,from,to) else swap(_,to,from)),
swap = (_,from,to)=> List.Range(_,0,from)
&{_{to}}
&List.Range(_,from+1,to-from-1)
&{_{from}}
&List.Range(_,to+1)
in
@Hugoberry
Hugoberry / JSONexpand.m
Last active February 22, 2017 17:11
JSON expansion in Power Query M
= List.Generate(
()=> [
can_expand=true,
level =1,
input =json,
output =[level.0=Record.FieldNames(json){0}]
],
each [can_expand],
each [
can_expand =Record.FieldValues([input]){0} is record,
@Hugoberry
Hugoberry / ExpandableColumns.m
Last active February 23, 2017 17:55
Get a list of expandable columns from a table input in Power Query
expandableColumns = (_) => List.Accumulate(
Table.ColumnNames(_),
{},
(s,c)=>s&(if Type.Is(Value.Type(Record.Field(_{0},c)), type record)
or Type.Is(Value.Type(Record.Field(_{0},c)), type list)
then {c}
else {}
)
),
columnHasConsistentType = (T,Cname) =>
@Hugoberry
Hugoberry / reverse8bit.m
Created February 27, 2017 23:06
Function to reverse an 8 bit number
reverse = (x) =>
Number.Mod(Number.BitwiseAnd((x* 0x0202020202),0x010884422010),1023),
@Hugoberry
Hugoberry / reverse32bit.m
Created February 27, 2017 23:08
Function for reversing the bits in a 32bit number
reverse32 = (x) =>
let
b0 = Number.BitwiseAnd(x,0xff),
b1 = Number.BitwiseShiftRight(Number.BitwiseAnd(x,0xff00),8),
b2 = Number.BitwiseShiftRight(Number.BitwiseAnd(x,0xff0000),16),
b3 = Number.BitwiseShiftRight(Number.BitwiseAnd(x,0xff000000),24)
in
Number.BitwiseOr(
Number.BitwiseOr(
Number.BitwiseShiftLeft(reverse(b0),24),
@Hugoberry
Hugoberry / CRC32lookupTable.m
Last active February 27, 2017 23:12
Precomputin the CRC32 lookup table
calculateCRClookup = (divident)=>
let
// move divident byte into MSB of 32Bit CRC
curByte = Number.BitwiseShiftLeft(divident,24)
in
List.Accumulate({0..7},
curByte,
(_curByte,_)=>
let
// if MSB set, shift left and XOR with polynomial = 0x4C11DB7
@Hugoberry
Hugoberry / swap.m
Created March 1, 2017 15:11
Swapping the list elements in positions _from and _to. Power Query implementation
List.Swap = (_,_from as number ,_to as number) =>
let
from = List.Min({_from,_to}),
to = List.Max({_from,_to})
in if from=to then _ else
List.Range(_,0,from)
&{_{to}}
&List.Range(_,from+1,to-from-1)
&{_{from}}
&List.Range(_,to+1)
@Hugoberry
Hugoberry / Web.BufferedContents.m
Created March 2, 2017 11:36
When the trace files shows duplicate Web.Content which might be triggered by some operations down the Power Query pipeline, it is a good idea to Buffer the Web.Contents first
(url) =>
let
BufferedWithMetadata = (binary)=>(try Binary.Buffer() otherwise null) meta Value.Metadata(binary),
response = BufferWithMetadata(Web.Contents(url,[ManualStatusHandling={404}])),
out = if Value.Metadata(response)[Response.Status]=404
then null
else response
in
out
@Hugoberry
Hugoberry / HEX2DEC.m
Created March 2, 2017 15:19
HEX2DEC one liner implementation in Power Query M
(hexString as text)
=> Expression.Evaluate(“0x”&hexString)