dynapate
2020-9-24 13:59:33

@dynapate has joined the channel


dynapate
2020-9-24 14:06:43

I’m trying to use a pkg called data-frame, and I’m trying to calculations on each row and I need access to the last row, would anyone know how to do this?



badkins
2020-9-24 14:44:14

I haven’t used the package, but it looks like df-row-count will get you the number of rows, and df-ref will extract values from a specific row.


dynapate
2020-9-24 14:53:28

I think fold is the way to go about it


dynapate
2020-9-24 14:53:35

since it let’s you calculate deltas


badkins
2020-9-24 14:57:50

I was just responding to the “I need access to the last row” aspect.


dynapate
2020-9-24 16:03:53

how does one do partial folds in racket?


laurent.orseau
2020-9-24 16:31:31

for loops usually


laurent.orseau
2020-9-24 16:33:15

But personally I like named lets, they are even more flexible, but easily more verbose


dynapate
2020-9-24 17:25:14

@laurent.orseau could you explain?

I have no idea how I’d ode that


dynapate
2020-9-24 17:25:24

as in I want a fold with more than 1 argument


dynapate
2020-9-24 17:25:36

so let’s say I want the fold operator to fold with addition but I want the last 7 values


soegaard2
2020-9-24 17:26:47

(let loop ([a initial-value] [xs xs] [ys ys[) (if (null? xs) a (loop (f a (first xs) (first ys)) (rest xs) (rest ys))))


dynapate
2020-9-24 17:27:34

I don’t know those constructs


dynapate
2020-9-24 17:27:40

but I’ll try it out


soegaard2
2020-9-24 17:30:02

An example: (reverse (let loop ([a '()] [xs '(1 2 3)] [ys '(a b c)]) (if (null? xs) a (loop (cons (list (first xs) (first ys)) a) (rest xs) (rest ys))))) the result is: '((1 a) (2 b) (3 c))


soegaard2
2020-9-24 17:37:59

As an example of a partial fold using for. Let’s add the last 3 elements of a list: #lang racket (define xs '(11 12 13 14 15 16 17 18 19 20)) (define m 3) ; number of elements to use (define n (- (length xs) m 1)) (for/fold ([a 0]) ([i (in-naturals)] [x xs] #:when (> i n)) (+ a x)) The result is 57


dyllongagnier
2020-9-24 17:44:26

The let loop syntax is the same as defining a recursive helper function so if that is confusing, you can always use a define based function instead. The advantage of let loop is that it gives a handy syntax for defining and invoking a recursive function with starting arguments.


dyllongagnier
2020-9-24 17:47:32

I think for loops are usually better than named lets because they impose more structure and communicate intent better. You also don’t need to worry about a for loop running forever as long as the input is finite (i.e. not a generator or generic stream).


dyllongagnier
2020-9-24 17:57:51

Not sure if folks have read this before, but https://oleksandrmanzyuk.wordpress.com/2014/06/18/from-object-algebras-to-finally-tagless-interpreters-2/\|https://oleksandrmanzyuk.wordpress.com/2014/06/18/from-object-algebras-to-finally-tagless-interpreters-2/ is pretty interesting. Essentially, the solutions presented to the expression problem is to create a typed DSL possibly with multiple interpreters which implement different functionality.


badkins
2020-9-24 18:27:25

(apply + (take-right xs m)) :)


laurent.orseau
2020-9-24 18:44:24

I agree with this. But quite often i start with a for loop, and it ends up being a complicated for/fold with #:result, #:break or filter values. Then i turn it into a named let and I can breathe.


sorawee
2020-9-24 18:55:02

For summing, you could also: (for/sum ([i (in-naturals)] [x xs] #:when (> i n)) x)


laurent.orseau
2020-9-24 21:10:37

Sometimes it’s the other way around, though…


notjack
2020-9-24 23:45:36

re: the sliding window operation: I should write a transducer for this


notjack
2020-9-24 23:50:20

> (transduce (list 1 2 3 4 5) (windowing 3) #:into into-list) (list (list 1 2 3) (list 2 3 4) (list 3 4 5))


rokitna
2020-9-25 01:49:12

Yeah, I was half-expecting you had a transducer that would already be perfect for this. :) I think that’d be a really good one. Sliding windows are great for convolutions like smoothing data or detecting edges, so they seem perfect for a streaming data library.


notjack
2020-9-25 01:50:13

I’m also thinking that it would be useful to be able to collect each window using a reducer, instead of just into a list


notjack
2020-9-25 01:50:37

> (transduce (list 1 2 3 4 5) (windowing 3 #:into into-sum) #:into into-list) (list 6 9 12)


343519265
2020-9-25 02:05:31

Assuming I have syntax classes which behaviors depend on parameters, how could I write a pattern-expander like (~parameterize ([p-expr expr]) <some pattern>)?


notjack
2020-9-25 02:06:55

that’s probably possible, but can you make the classes depend on regular arguments instead?


343519265
2020-9-25 02:21:10

That seems possible if make it a optional argument, but it also seems very easy to forget to pass it properly


343519265
2020-9-25 02:23:41

If I make it mandatory, the code probably will be very verbose


notjack
2020-9-25 02:31:49

I don’t think syntax classes support optional arguments


notjack
2020-9-25 02:33:15

re: windowing transducer: IT WORKS!


notjack
2020-9-25 02:33:59

and it supports configuring how you collect each window


gfb
2020-9-25 02:36:11

Not racket-specific, but does anyone happen to know of a standard definition (or reference for discussion) of higher-order function beyond “takes a function as argument or produces a function” that accounts for it being both too broad and too specific to capture the intent in various contexts. E.g. if we want to rule out the identity function and include functions that call the elements of a list of functions. Something like “might call a function extracted from an argument or produce a result containing a function not extracted from the arguments” maybe?


343519265
2020-9-25 02:38:08

Indeed it does


samth
2020-9-25 02:38:39

I think you’re going to want to talk about types for that definition


samth
2020-9-25 02:39:03

ie, a function whose domain or range involves a function type


gfb
2020-9-25 02:46:30

Thanks, that answers the bigger question of how I should talk about the standard-as-in-common definition.


notjack
2020-9-25 03:03:36

I don’t think that would be enough, because: newtype Predicate a = Predicate (a -&gt; Boolean) negatePredicate : Predicate a -&gt; Predicate a


notjack
2020-9-25 03:04:52

whoa, and they support keyword arguments too?? today I learned


samth
2020-9-25 03:13:46

I don’t see how that changes anything — Predicate includes a function type.


notjack
2020-9-25 03:15:15

it means you need to be able to peek inside the possibly-opaque types within a function’s type


343519265
2020-9-25 03:15:19

yes


notjack
2020-9-25 03:15:43

imagine Predicate a was defined in a library that wanted to hide its representation


samth
2020-9-25 03:16:02

then it wouldn’t be higher order


samth
2020-9-25 03:16:42

if i write a library of church numerals, and hide the representation completely, then that doesn’t make add1 higher order


notjack
2020-9-25 03:31:05

huh, that makes sense but feels weird


notjack
2020-9-25 06:26:33

documented and added to rebellion :tada:


notjack
2020-9-25 06:35:48

neat facts: the space consumed is linear in the window size and the space consumed by window-reducer, so if you have a constant-space reducer like into-sum, you can use a very large window size without getting any weird quadratic behavior