How to create a plan in a function - drake-r-package

I'd like to use a function to create a drake plan. See MWE:
plan_func <- function(param) {
drake::drake_plan(
myparam = param
)
}
I would like
plan_func("a")
to give
# A tibble: 1 x 2
target command
<chr> <expr_lst>
1 myparam "a"
but instead, it gives
> plan_func("a")
# A tibble: 1 x 2
target command
<chr> <expr_lst>
1 myparam param
It feels like this is an NSE problem. Can someone give a friendly hint how to get this right?
My appreciation in advance!

drake_plan() supports tidy evaluation, so you can write !!param in the plan.
library(drake)
plan_func <- function(param) {
drake::drake_plan(
myparam = !!param
)
}
plan_func("a")
#> # A tibble: 1 x 2
#> target command
#> <chr> <expr_lst>
#> 1 myparam "a"
Created on 2020-06-02 by the reprex package (v0.3.0)
Trickier situations like https://github.com/ropensci/drake/issues/1251 might require you to turn off the tidy_eval and transform arguments of drake_plan().
Edit
To splice multiple arguments into a function, use triple-bang (!!!) instead of bang-bang (!!):
library(drake)
plan_func <- function(param) {
drake_plan(
myparam = f(!!param)
)
}
plan_func(c("a", "b"))
#> # A tibble: 1 x 2
#> target command
#> <chr> <expr>
#> 1 myparam f(c("a", "b"))
plan_func <- function(param) {
drake_plan(
myparam = f(!!!param)
)
}
plan_func(c("a", "b"))
#> # A tibble: 1 x 2
#> target command
#> <chr> <expr>
#> 1 myparam f("a", "b")
Created on 2020-06-02 by the reprex package (v0.3.0)

Related

Why does direct code quotation of System.Func yield a delegate and spliced quotation yields a lambda?

I was surprised that the direct code quotation
<# System.Func<int,int>( fun x -> x+1 ) #>
produces an Expr.NewDelegate and the spliced code quotation
<# fun x -> x+1 #> |> fun e -> <# System.Func<int,int>( %e ) #>
produces an Expr.Lambda, I had assumed these expressions were equivalent.
Is that expected? Is there an explanation why?

What does | and || mean in Erlang?

I have checked the erlang websites operators etc but i cannot find what || and | Means.
I read somewhere that || means "such that" but what does just one " | " mean?
| is the "cons" operator: It puts an element in front of a list:
1> [1 | [2,3]].
[1,2,3]
2> [[1, 2] | [3,4,5]].
[[1,2],3,4,5]
|| is used in list-comprehensions. In its simplest form, it can be used as a short-hand for map:
3> [2 * X || X <- [1,2,3]].
[2,4,6]
But it becomes much more handy when you want to write multiple generators, creating the cartesian product:
4> [{X, Y} || X <- [1,2,3], Y <- [4, 5, 6]].
[{1,4},{1,5},{1,6},{2,4},{2,5},{2,6},{3,4},{3,5},{3,6}]
You can also do filter along the way. Compare:
5> [X+Y || X <- [1,2,3], Y <- [4,5,6]].
[5,6,7,6,7,8,7,8,9]
to:
6> [X+Y || X <- [1,2,3], Y <- [4,5,6], X+Y > 6].
[7,7,8,7,8,9]
The | operator is essential, in the sense that it is the canonical way how you construct a new list out of an existing head element and a tail of the list. The same notation also works in pattern matching, i.e., it is also how you deconstruct a list.
On the other hand, list comprehensions are mostly syntactic sugar: They can be written using regular function applications and hence is not fundamental to the language. But they can significantly improve readability by getting rid of syntactic noise, mimicking set-comprehension like mathematical notation directly within the language.

Change Enum.map from Elixir to Erlang

I need to convert Elixir function into Erlang function:
In Elixir I have:
Enum.map(0..n, fn i-> fun(i) end)
And I need to re-write to Erlang.
Any Idea? Thanks
Erlang doesn't have a single generic function that can handle mapping over any data structure like Enum.map in Elixir. The simplest way to do this would be to use lists:seq to generate the list and lists:map:
1> lists:map(fun(X) -> X * X end, lists:seq(0, 10)).
[0,1,4,9,16,25,36,49,64,81,100]
Using list comprehensions:
[ F(X) || X <- lists:seq(0, 10) ].
aka
[ X*X || X <- lists:seq(0, 10) ].

named implementation to default implementation

I defined a named implementation for the typeclass Ord for type Int.
[mijnOrd] Ord Int where
compare n1 n2 = ...
How can I import this named implementation and use it as "default"
so in another module I want to import this implementation
Mark it as default
And use it as if it was default
--
sort [1,5,2] -- output without importing as default: [1,2,5]
sort [1,5,2] -- output with importing as default: [5,2,1]
Is this possible in Idris?
This is possible since Idris 0.12 using using-blocks:
Export your named interface in one module, say MyOrd.idr:
module MyOrd
-- Reverse order for `Nat`
export
[myOrd] Ord Nat where
compare Z Z = EQ
compare Z (S k) = GT
compare (S k) Z = LT
compare (S k) (S j) = compare #{myOrd} k j
Then just import it in another module and wrap everything that should use it as default in a corresponding using-block like so:
-- Main.idr
module Main
import MyOrd
using implementation myOrd
test : List Nat -> List Nat
test = sort
main : IO ()
main = putStrLn $ show $ test [3, 1, 2]
This should print [3, 2, 1].

-> operator in Clojure

Is the -> operator in Clojure (and what is this operator called in Clojure-speak?) equivalent to the pipeline operator |> in F#? If so, why does it need such a complex macro definition, when (|>) is just defined as
let inline (|>) x f = f x
Or if not, does F#'s pipeline operator exist in Clojure, or how would you define such an operator in Clojure?
No, they are not the same. Clojure doesn't really have a need for |> because all function calls are enclosed in lists, like (+ 1 2): there's no magic you could do to make 1 + 2 work in isolation.1
-> is for reducing nesting and simplifying common patterns. For example:
(-> x (assoc :name "ted") (dissoc :size) (keys))
Expands to
(keys (dissoc (assoc x :name "ted") :size))
The former is often easier to read, because conceptually you're performing a series of operations on x; the former code is "shaped" that way, while the latter needs some mental unraveling to work out.
1 You can write a macro that sorta makes this work. The idea is to wrap your macro around the entire source tree that you want to transform, and let it look for |> symbols; it can then transform the source into the shape you want. Hiredman has made it possible to write code in a very Haskell-looking way, with his functional package.
It's called the "thread" operator. It's written as a macro as opposed to a normal function for performance reasons and so that it can provide a nice syntax - i.e. it applies the transformation at compile time.
It's somewhat more powerful than the |> operator you describe, as it's intended to pass a value through several functions, where each successive value is "inserted" as the first parameter of the following function calls. Here's a somewhat contrived example:
(-> [1]
(concat [2 3 4])
(sum)
((fn [x] (+ x 100.0))))
=> 110.0
If you want to define a function exactly like the F# operator you have described, you can do:
(defn |> [x f] (f x))
(|> 3 inc)
=> 4
Not sure how useful that really is, but there you are anyway :-)
Finally, if you want to pass a value through a sequence of functions, you can always do something like the following in clojure:
(defn pipeline [x & fns]
((apply comp fns) x))
(pipeline 1 inc inc inc inc)
=> 5
It is also worth noting that there is a ->> macro which will thread the form as the last argument:
(->> a (+ 5) (let [a 5] ))
The Joy of Clojure, chapter 8.1 talks about this subject a bit.
When reading source code (especially when speaking), I always pronounce the -> operator as "thread-first", and the ->> operator as "thread-last".
Keep in mind that there is now an operator as-> which is more flexible than either -> or ->>. The form is:
(as-> val name (form1 arg1 name arg2)...)
The value val is evaluated and assigned to the placeholder symbol name, which the user can place in ANY position in the following forms. I usually choose the word "it" for the placeholder symbol. We can mimic thread-first -> like so:
user=> (-> :a
(vector 1))
[:a 1]
user=> (as-> :a it
(vector it 1) )
[:a 1]
We can mimic thread-last ->> like so:
user=> (->> :a
(vector 2))
[2 :a]
user=> (as-> :a it
(vector 2 it) )
[2 :a]
Or, we can combine them in a single expression:
user=> (as-> :a it
(vector it 1)
(vector 2 it))
[2 [:a 1]]
user=> (as-> :a it
(vector it 1)
(vector 2 it)
(vector "first" it "last"))
["first" [2 [:a 1]] "last"]
I use this last form so much I have made a new operator it-> in the Tupelo Library:
(it-> 1
(inc it) ; thread-first or thread-last
(+ it 3) ; thread-first
(/ 10 it) ; thread-last
(str "We need to order " it " items." ) ; middle of 3 arguments
;=> "We need to order 2 items." )

Resources