Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to create a function accepting a closure ? #392

Unanswered
buixor asked this question in General
Discussion options

Hello,

I would like to know how to add a function accepting closure as an argument.

For example, I want to create a function flatten( something, { #.some_field }):

  • something would be a []map[string]string
  • and it would return a []string containing the some_field values found in the something array

To achieve this, I believe closure would be the best way. Is it something that can be done ? (I didn't find other examples except the builtins all/one/etc. but I don't see how to transpose this to a function I could expose).

Thanks!

You must be logged in to vote

Replies: 1 comment 3 replies

Comment options

You can just use this:

map(something, .some_field)

Brackets { and }, as well as # char is optional.

You must be logged in to vote
3 replies
Comment options

Hello,

Sorry, the flatten was just an example.

My question is really how/if I can create a function that accepts closure argument 😄

Thanks and sorry if the question was unclear

Comment options

This is not possible. Those predicates (all, none, map, filter, etc) and closures are tight together. Think of them as language keywords (like for, while, do {...} while).

It may be cool to add some API to allow to use closures somehow.

What is your use case? What kind of problem you trying to solve?

Comment options

I was looking at the same today. My specific usecause is for a custom filter/count/sum where I want the closure to be a predicate pushdown to the left-hand side, which is a function, that does some sql queries. I was wondering what the most ergonomic way to implement this would be.

I primarily want to avoid moving data unnecessarily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /