Skip to content

Instantly share code, notes, and snippets.

@jrfondren

jrfondren/output.txt

Last active Apr 23, 2019
Embed
What would you like to do?
@[1, 2, 3, 4, 5, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
type
Shard* = tuple
data: ref seq[byte]
index: int
proc shardAt*(arr: ref seq[byte], i: int): Shard =
(data: arr, index: i)
iterator items*(sh: Shard): byte =
for i in sh.index ..< sh.index + 32:
yield sh.data[i]
proc onShard*(sh: Shard, f: proc (sh: var openArray[byte]): void) =
f(toOpenArray(sh.data[], sh.index, sh.index + 32))
import shard
var buf: ref seq[byte]
new(buf)
for i in 1 .. 100: buf[].add(byte(i))
var x = shardAt(buf, 5)
onShard(x, proc (sh: var openArray[byte]) =
for i in 0 ..< sh.len:
sh[i] = 0xFF)
echo buf[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.