
@dynapate has joined the channel

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?


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.

I think fold is the way to go about it

since it let’s you calculate deltas

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

how does one do partial folds in racket?

for
loops usually

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

@laurent.orseau could you explain?
I have no idea how I’d ode that

as in I want a fold with more than 1 argument

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

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

I don’t know those constructs

but I’ll try it out

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))

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

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.

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).

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.

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

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.

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

Sometimes it’s the other way around, though…

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

> (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))

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.

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

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

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

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

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

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

I don’t think syntax classes support optional arguments

re: windowing transducer: IT WORKS!

and it supports configuring how you collect each window

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?

Indeed it does

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

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

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

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

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

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

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

yes

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

then it wouldn’t be higher order

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

huh, that makes sense but feels weird

documented and added to rebellion :tada:

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