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

Appearance settings
Discussion options

Hi @antonmedv!

I'm trying to port some of the functions provided by the lo package to have more capabilities of collection types manipulation. But I'm having a hard time trying to represent the function argument as an expression. It compiles but throws a runtime error.

Expr version: v1.17.5
Go version: go1.24.9 linux/amd64

Example of the Expr function:

import (
	"github.com/expr-lang/expr"
	"github.com/samber/lo"
)

const sliceToMapFuncParamsLen = 2

func sliceToMapFunc() expr.Option {
	return expr.Function(
		"SliceToMap",
		func(params ...any) (any, error) {
			if len(params) != 2 {
				return nil, invalidParametersNumberError("SliceToMap", sliceToMapFuncParamsLen, len(params))
			}

			slice, ok := params[0].([]any)
			if !ok {
				return nil, invalidParameterTypeError("SliceToMap", 0, "[]any", TypeOf(params[0]))
			}

			fn, ok := params[1].(func(any) (string, any))
			if !ok {
				return nil, invalidParameterTypeError("SliceToMap", 1, "func(any) (string, any)", TypeOf(params[1]))
			}

			return lo.SliceToMap(slice, fn), nil
		},
		new(func([]any, func(any) (string, any)) (any, error)),
	)
}

Expression examples:

SliceToMap(.data, {{string(#.key):#.value}})
SliceToMap(.data, {#.key,#.value})
You must be logged in to vote

This {#.key,#.value} is not a function. This is a predicate. Only afavialable only inside map/function and other builtins. It's not avialble for custom functions.

Replies: 1 comment

Comment options

This {#.key,#.value} is not a function. This is a predicate. Only afavialable only inside map/function and other builtins. It's not avialble for custom functions.

You must be logged in to vote
0 replies
Answer selected by antonmedv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #858 on November 07, 2025 14:44.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.