| 1 |
package dstream |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
) |
| 7 |
|
| 8 |
func ExampleToCSV() { |
| 9 |
|
| 10 |
data := `Food,Type,Weight,Price |
| 11 |
Banana,Fruit,13,9 |
| 12 |
Cucumber,Vegetable,15,5 |
| 13 |
Cheese,Dairy,12,35 |
| 14 |
Lamb,Meat,40,76 |
| 15 |
` |
| 16 |
|
| 17 |
b := bytes.NewBuffer([]byte(data)) |
| 18 |
types := []VarType{ |
| 19 |
{"Food", String}, |
| 20 |
{"Type", String}, |
| 21 |
{"Weight", Float64}, |
| 22 |
} |
| 23 |
da := FromCSV(b).SetTypes(types).HasHeader().Done() |
| 24 |
|
| 25 |
var buf bytes.Buffer |
| 26 |
_ = ToCSV(da).SetWriter(&buf).FloatFmt("%.0f").Done() |
| 27 |
|
| 28 |
fmt.Printf("%s\n", buf.String()) |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
func ExampleFromCSV() { |
| 39 |
|
| 40 |
data := `Food,Type,Weight,Price |
| 41 |
Banana,Fruit,13,9 |
| 42 |
Cucumber,Vegetable,15,5 |
| 43 |
Cheese,Dairy,12,35 |
| 44 |
Lamb,Meat,40,76 |
| 45 |
` |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
b := bytes.NewBuffer([]byte(data)) |
| 50 |
|
| 51 |
types := []VarType{ |
| 52 |
{"Food", String}, |
| 53 |
{"Type", String}, |
| 54 |
{"Weight", Float64}, |
| 55 |
} |
| 56 |
da := FromCSV(b).SetTypes(types).HasHeader().Done() |
| 57 |
da.Next() |
| 58 |
|
| 59 |
y := da.Get("Type").([]string) |
| 60 |
fmt.Printf("%v\n", y) |
| 61 |
|
| 62 |
x := da.Get("Weight").([]float64) |
| 63 |
fmt.Printf("%v\n", x) |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
func ExampleMutate() { |
| 71 |
|
| 72 |
data := `V1,V2,V3,V4 |
| 73 |
1,2,3,4 |
| 74 |
2,3,4,5 |
| 75 |
3,4,5,6 |
| 76 |
4,5,6,7 |
| 77 |
` |
| 78 |
|
| 79 |
|
| 80 |
timesTwo := func(x interface{}) { |
| 81 |
v := x.([]float64) |
| 82 |
for i := range v { |
| 83 |
v[i] *= 2 |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
types := []VarType{ |
| 88 |
{"V1", Float64}, |
| 89 |
{"V2", Float64}, |
| 90 |
{"V3", Float64}, |
| 91 |
{"V4", Float64}, |
| 92 |
} |
| 93 |
b := bytes.NewBuffer([]byte(data)) |
| 94 |
da := FromCSV(b).SetTypes(types).HasHeader().Done() |
| 95 |
da = Mutate(da, "V2", timesTwo) |
| 96 |
|
| 97 |
da.Next() |
| 98 |
|
| 99 |
y := da.Get("V2") |
| 100 |
fmt.Printf("%v\n", y) |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
func ExampleFilter() { |
| 107 |
|
| 108 |
data := `V1,V2,V3,V4 |
| 109 |
1,2,3,4 |
| 110 |
2,0,4,5 |
| 111 |
3,4,5,6 |
| 112 |
4,0,6,7 |
| 113 |
` |
| 114 |
|
| 115 |
|
| 116 |
f := func(v map[string]interface{}, b []bool) { |
| 117 |
x := v["V2"].([]float64) |
| 118 |
for i := range x { |
| 119 |
b[i] = x[i] != 0 |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
types := []VarType{ |
| 124 |
{"V1", Float64}, |
| 125 |
{"V2", Float64}, |
| 126 |
{"V3", Float64}, |
| 127 |
{"V4", Float64}, |
| 128 |
} |
| 129 |
b := bytes.NewBuffer([]byte(data)) |
| 130 |
da := FromCSV(b).SetTypes(types).HasHeader().Done() |
| 131 |
da = Filter(da, f) |
| 132 |
|
| 133 |
da.Next() |
| 134 |
|
| 135 |
y := da.Get("V1") |
| 136 |
fmt.Printf("%v\n", y) |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
func ExampleSegment() { |
| 143 |
|
| 144 |
data := `V1,V2,V3,V4 |
| 145 |
1,2,3,4 |
| 146 |
1,0,4,5 |
| 147 |
2,4,5,6 |
| 148 |
3,0,6,7 |
| 149 |
` |
| 150 |
|
| 151 |
types := []VarType{ |
| 152 |
{"V1", Float64}, |
| 153 |
{"V2", Float64}, |
| 154 |
{"V3", Float64}, |
| 155 |
{"V4", Float64}, |
| 156 |
} |
| 157 |
b := bytes.NewBuffer([]byte(data)) |
| 158 |
da := FromCSV(b).SetTypes(types).HasHeader().Done() |
| 159 |
da = Segment(da, "V1") |
| 160 |
|
| 161 |
for da.Next() { |
| 162 |
y := da.Get("V2") |
| 163 |
fmt.Printf("%v\n", y) |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
func ExampleGenerate() { |
| 173 |
|
| 174 |
data := `V1,V2,V3,V4 |
| 175 |
1,2,3,4 |
| 176 |
1,0,4,5 |
| 177 |
2,4,5,6 |
| 178 |
3,0,6,7 |
| 179 |
` |
| 180 |
|
| 181 |
f := func(v map[string]interface{}, x interface{}) { |
| 182 |
v1 := v["V1"].([]float64) |
| 183 |
v2 := v["V2"].([]float64) |
| 184 |
y := x.([]float64) |
| 185 |
for i := range v1 { |
| 186 |
y[i] = v1[i] + v2[i] |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
b := bytes.NewBuffer([]byte(data)) |
| 191 |
|
| 192 |
types := []VarType{ |
| 193 |
{"V1", Float64}, |
| 194 |
{"V2", Float64}, |
| 195 |
{"V3", Float64}, |
| 196 |
{"V4", Float64}, |
| 197 |
} |
| 198 |
da := FromCSV(b).SetTypes(types).HasHeader().Done() |
| 199 |
da = Generate(da, "V1p2", f, Float64) |
| 200 |
|
| 201 |
for da.Next() { |
| 202 |
y := da.Get("V1p2") |
| 203 |
fmt.Printf("%v\n", y) |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
func ExampleDiffChunk() { |
| 211 |
|
| 212 |
data := `V1,V2,V3,V4 |
| 213 |
1,2,3,4 |
| 214 |
1,0,4,5 |
| 215 |
2,4,5,6 |
| 216 |
3,0,6,8 |
| 217 |
3,1,5,9 |
| 218 |
` |
| 219 |
|
| 220 |
b := bytes.NewBuffer([]byte(data)) |
| 221 |
types := []VarType{ |
| 222 |
{"V1", Float64}, |
| 223 |
{"V2", Float64}, |
| 224 |
{"V3", Float64}, |
| 225 |
{"V4", Float64}, |
| 226 |
} |
| 227 |
da := FromCSV(b).SetTypes(types).HasHeader().Done() |
| 228 |
da = DiffChunk(da, map[string]int{"V2": 1, "V4": 2}) |
| 229 |
|
| 230 |
for da.Next() { |
| 231 |
y := da.Get("V2$d1") |
| 232 |
fmt.Printf("%v\n", y) |
| 233 |
y = da.Get("V4$d2") |
| 234 |
fmt.Printf("%v\n", y) |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
func ExampleLagChunk() { |
| 243 |
|
| 244 |
data := `1,2,3,4 |
| 245 |
2,3,4,5 |
| 246 |
3,4,5,6 |
| 247 |
4,5,6,7 |
| 248 |
` |
| 249 |
|
| 250 |
b := bytes.NewBuffer([]byte(data)) |
| 251 |
|
| 252 |
types := []VarType{ |
| 253 |
{"V1", Float64}, |
| 254 |
{"V2", Float64}, |
| 255 |
{"V3", Float64}, |
| 256 |
{"V4", Float64}, |
| 257 |
} |
| 258 |
da := FromCSV(b).SetTypes(types).Done() |
| 259 |
da = LagChunk(da, map[string]int{"V2": 2}) |
| 260 |
|
| 261 |
da.Next() |
| 262 |
|
| 263 |
y := da.Get("V2[0]") |
| 264 |
fmt.Printf("%v\n", y) |
| 265 |
y = da.Get("V2[-1]") |
| 266 |
fmt.Printf("%v\n", y) |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
func ExampleJoin() { |
| 274 |
|
| 275 |
data1 := `V1,V2,V3,V4 |
| 276 |
1,2,3,4 |
| 277 |
1,3,4,5 |
| 278 |
3,4,5,6 |
| 279 |
3,5,6,7 |
| 280 |
` |
| 281 |
|
| 282 |
data2 := `V1,V2,V3 |
| 283 |
1,2,3 |
| 284 |
1,3,4 |
| 285 |
1,4,5 |
| 286 |
3,5,6 |
| 287 |
` |
| 288 |
|
| 289 |
data3 := `V1,V2,V3,V4,V5 |
| 290 |
2,2,3,5,6 |
| 291 |
2,3,4,7,5 |
| 292 |
3,4,5,3,4 |
| 293 |
4,5,6,2,3 |
| 294 |
` |
| 295 |
|
| 296 |
names := [][]string{{"V1", "V2", "V3", "V4"}, |
| 297 |
{"V1", "V2", "V3"}, |
| 298 |
{"V1", "V2", "V3", "V4", "V5"}} |
| 299 |
var da []Dstream |
| 300 |
|
| 301 |
for j, data := range []string{data1, data2, data3} { |
| 302 |
|
| 303 |
b := bytes.NewBuffer([]byte(data)) |
| 304 |
|
| 305 |
var types []VarType |
| 306 |
for _, na := range names[j] { |
| 307 |
types = append(types, VarType{na, Float64}) |
| 308 |
} |
| 309 |
|
| 310 |
d := FromCSV(b).SetTypes(types).HasHeader().Done() |
| 311 |
d = Convert(d, "V1", Uint64) |
| 312 |
d = Segment(d, "V1") |
| 313 |
da = append(da, d) |
| 314 |
} |
| 315 |
|
| 316 |
join := NewJoin(da, []string{"V1", "V1", "V1"}) |
| 317 |
|
| 318 |
for join.Next() { |
| 319 |
fmt.Printf("%v\n", da[0].GetPos(0)) |
| 320 |
if join.Status[1] { |
| 321 |
fmt.Printf("%v\n", da[1].GetPos(0)) |
| 322 |
} |
| 323 |
if join.Status[2] { |
| 324 |
fmt.Printf("%v\n\n", da[2].GetPos(0)) |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
func ExampleRegroup() { |
| 337 |
|
| 338 |
data := `V1,V2,V3 |
| 339 |
1,2,3 |
| 340 |
3,3,4 |
| 341 |
2,4,5 |
| 342 |
2,5,6 |
| 343 |
5,2,3 |
| 344 |
0,3,4 |
| 345 |
1,4,5 |
| 346 |
5,5,6 |
| 347 |
` |
| 348 |
|
| 349 |
b := bytes.NewBuffer([]byte(data)) |
| 350 |
|
| 351 |
types := []VarType{ |
| 352 |
{"V1", Float64}, |
| 353 |
{"V2", Float64}, |
| 354 |
{"V3", Float64}, |
| 355 |
} |
| 356 |
|
| 357 |
d := FromCSV(b).SetTypes(types).HasHeader().Done() |
| 358 |
d = Convert(d, "V1", Uint64) |
| 359 |
d = Regroup(d, "V1", true) |
| 360 |
|
| 361 |
for d.Next() { |
| 362 |
fmt.Printf("%v\n", d.GetPos(0)) |
| 363 |
fmt.Printf("%v\n", d.GetPos(1)) |
| 364 |
fmt.Printf("%v\n\n", d.GetPos(2)) |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
} |