In my puppet class i use 3 create_resources. I want to execute these create_resources in order. Thus there are relationships between each other
create_resources(change_config::cr1, $resource)
create_resources(change_config::cr2, $resource)
create_resources(change_config::cr3, $resource)
cr3 should be executed after cr2 and cr2 should be after cr1.
Is there a way to do this ?
Regards,
Malintha
You can use Puppet Collectors here. Simply add this line into your manifest:
Change_config::Cr1<| |> -> Change_config::Cr2<| |> -> Change_config::Cr3<| |>
It will order all Cr1's before 2's before 3s. You can even put extra filtering inside the <| |> like
Change_config::Cr1<| title == 'some_name' |> -> Change_config::Cr1<| <| title != 'some_name' |>
Related
I am very new to F# and Kattis. I have tried this simple problem "Which is greater" at Kattis. Link is here: https://open.kattis.com/problems/whichisgreater
I have tried with this code:
open System
let a = Console.Read()
let b = Console.Read()
if a > b then Console.WriteLine "1" else Console.WriteLine "0"
But I still get wrong answer. Anybody who can help on how to handle inputs and outputs in Kattis for F#? Maybe some simple examples can be made available?
The following is accepted by Kattis:
open System
let line = Console.ReadLine().Split ' '
let a = int64 line.[0]
let b = int64 line.[1]
Console.WriteLine(if a > b then 1 else 0)
Here, we read the line, split it on a space character into two numbers, compare them and print the required result.
It looks like they're using an old version of the F# compiler, so you have to specify an explicit entry point. Here's their sample F# solution for a different problem:
open System
[<EntryPoint>]
let main argv =
(fun _ -> Console.ReadLine()) |>
Seq.initInfinite |>
Seq.takeWhile ((<>) null) |>
Seq.iter
(fun (s : string) ->
let arr = s.Split([|' '|])
let a = int64 arr.[0]
let b = int64 arr.[1]
/// solve test case and output answer
printfn "%d" (abs (a - b))
)
0
I think that should give you enough info to solve the "which is greater" problem you're looking at. (Note that Console.Read only reads a single character, so it's not what you want for this problem. Instead, you probably want to read in the entire line, then split it into two strings at the blank space, then convert each of those strings into an integer. Coincidentally, the sample code I pasted above does something similar.)
i need to retrieve the key whose value contains a string "TRY"
:CAB "NAB/TRY/FIGHT.jar"
so in this case the output should be :CAB .
I am new to Clojure, I tried a few things like .contains etc but I could not form the exact function for the above problem.its easier in few other languages like python but I don't know how to do it in Clojure.
Is there a way to retrieve the name of the key ?
for can also filter with :when. E.g.
(for [[k v] {:FOO "TRY" :BAR "BAZ"}
:when (.contains v "TRY")]
k)
First, using .contains is not recommended - first, you are using the internals of the underlying language (Java or JavaScript) without need, and second, it forces Clojure to do a reflection as it cannot be sure that the argument is a string.
It's better to use clojure.string/includes? instead.
Several working solutions have been already proposed here for extracting a key depending on the value, here is one more, that uses the keep function:
(require '[clojure.string :as cs])
(keep (fn [[k v]] (when (cs/includes? v "TRY") k))
{:CAB "NAB/TRY/FIGHT.jar" :BLAH "NOWAY.jar"}) ; => (:CAB)
The easiest way is to use the contains method from java.lang.String. I'd use that to map valid keys, and then filter to remove all nil values:
(filter some?
(map (fn [[k v]] (when (.contains v "TRY") k))
{:CAB "NAB/TRY/FIGHT.jar" :BLAH "NOWAY.jar"}))
=> (:CAB)
If you think there is at most one such matching k/v pair in the map, then you can just call first on that to get the relevant key.
You can also use a regular expression instead of .contains, e.g.
(fn [[k v]] (when (re-find #"TRY" v) k))
You can use some on your collection, some will operate in every value in your map a given function until the function returns a non nil value.
We're gonna use the function
(fn [[key value]] (when (.contains values "TRY") key))
when returns nil unless the condition is matched so it will work perfectly for our use case. We're using destructuring in the arguments of the function to get the key and value. When used by some, your collection will indeed be converted to a coll which will look like
'((:BAR "NAB/TRY/FIGHT.jar"))
If your map is named coll, the following code will do the trick
(some
(fn [[key value]] (when (.contains value "TRY") key))
coll)
How do i use the split method in f# correctly?
The code:
let a = "abc def"
let b = a.Split [|' '|]
Console.WriteLine("{0}", b)
does return
"System.String[]"
i tryed many variations of:
.Split([|' '|])
The language doc & other questions don't really help.
i want b to be:
["abc","def"]
thanks for helping.
Your Console.WriteLine is the actual problem here, not the fact you get an array vs a list. It's just that the list overrides ToString, so that happens to make your code work.
You can use either of these to get the same effect with an array:
Console.WriteLine(sprintf "%A" b)
printfn "%A" b
I think you want a list as result but .Split returns an array of strings, not a list of strings.
If you want a list just call Array.toList after calling .Split:
let b = a.Split [|' '|] |> Array.toList
UPDATE
Based on your comments, if you are happy with the result as an Array then all you need to change is the way you print it.
One way is to use sprintf to pretty-print the array, as stated in my comments and in the other answer. Here's another way:
open System
let a = "abc def"
let b = a.Split [|' '|]
Console.WriteLine(String.Join(";", b))
I need to create a function that basically works like this :
insert_char("string" 'x') outputs "sxtxrxixnxg".
So here is my reasoning :
Create a list with every single character in the string :
let inserer_car(s, c) =
let l = ref [] in
for i = 0 to string.length(s) - 1 do
l := s.[i] :: !l
done;
Then, I want to use List.map to turn it into a list like ['s', 'x', 't', 'x' etc.].
However, I don't really know how to create my function to use with map. Any help would be appreciated!
I'm a beginner in programming and especially in ocaml! so feel free to assume I'm absolutely ignorant.
If you were using Core, you could write it like this:
open Core.Std
let insert_char s c =
String.to_list s
|> (fun l -> List.intersperse l c)
|> String.of_char_list
Or, equivalently:
let insert_char s c =
let chars = String.to_list s in
let interspersed_chars = List.intersperse chars c in
String.of_char_list interspersed_chars
This is just straightforward use of existing librariies. If you want the implementation of List.intersperse, you can find it here. It's quite simple.
A map function creates a copy of a structure with different contents. For lists, this means that List.map f list has the same length as list. So, this won't work for you. Your problem requires the full power of a fold.
(You could also solve the problem imperatively, but in my opinion the reason to study OCaml is to learn about functional programming.)
Let's say you're going to use List.fold_left. Then the call looks like this:
let result = List.fold_left myfun [] !l
Your function myfun has the type char list -> char -> char list. In essence, its first parameter is the result you've built so far and its second parameter is the next character of the input list !l. The result should be what you get when you add the new character to the list you have so far.
At the end you'll need to convert a list of characters back to a string.
The current setup goes something like this
array
|> Seq.map (fun item -> async { return f item})
|> Async.Parallel
|> Async.RunSynchronously
The problem is, this tends to create too many threads and crash the application periodically.
How to limit the number of threads in this case (to, say, Environment.ProcessorCount)?
Since 2018 pull request there is a built-in option in F# Core via a second overload of Async.Parallel F# doc
array
|> Seq.map (fun item -> async { return f item})
|> fun computations -> Async.Parallel(computations, maxDegreeOfParallelism = 20)
|> Async.RunSynchronously
If you want to parallelize CPU-intensive calculation that takes an array (or any sequence) as an input, then it may be a better idea to use PSeq module from the F# PowerPack (which is available only on .NET 4.0 though). It provides a parallel versions of many standard Array.xyz functions. For more information, you can also look at F# translation of Parallel Programming with .NET samples.
The code to solve your problem would be a bit simpler than using workflows:
array |> PSeq.map f
|> PSeq.toArray
Some differences between the two options are:
PSeq is created using Task Parallel Library (TPL) from .NET 4.0, which is optimized for working with a large number of CPU-intensive tasks.
Async is implemented in F# libraries and supports asynchronous (non-blocking) operations such as I/O in the concurrently running operations.
In summary, if you need asynchronous operations (e.g. I/O) then Async is the best option. If you have a large number of CPU-intensive tasks, then PSeq may be a better choice (on .NET 4.0)
Here is a working example of how to do this using a Semaphore, in the spirit of Brian's suggestion:
open System
let throttle n fs =
seq { let n = new Threading.Semaphore(n, n)
for f in fs ->
async { let! ok = Async.AwaitWaitHandle(n)
let! result = Async.Catch f
n.Release() |> ignore
return match result with
| Choice1Of2 rslt -> rslt
| Choice2Of2 exn -> raise exn
}
}
let f i = async { printfn "start %d" i
do! Async.Sleep(2000)
}
let fs = Seq.init 10 f
fs |> throttle 2 |> Async.Parallel |> Async.RunSynchronously |> ignore
You could introduce your own throttle along these lines:
let throttle = makeThrottle(8)
array
|> Seq.map (fun item -> async { do! throttle.Wait()
return f item})
|> Async.Parallel
|> Async.RunSynchronously
makeThrottle() would not be too hard to write, but it would incur a little synchronization overhead. If you are trying to parallelize so many things that you're running out of memory, the throttle overhead is likely to be a non-issue. (Let me know if you need a sample for this kind of code.)
Finally, if this is really crashing things, it smells like you may be doing something wrong. The ThreadPool typically (but not always) does a good job managing itself. But in various circumstances, designing your own throttle may be valuable to your app anyway.