Open ghost opened 10 years ago
Yeah, I guess it is a matter of preference :). I picked my way since series/parallel circuit elements are usually colinear/parallel respectively when you actually build the circuit. I see where you are coming from though!
Well it might be a matter of preference but I will always be careful whenever I overwrite a standard operator, in your case the Bitwise Operators (F#), as it might give undesired behavior for people using your code :-)
p.s.: I got a bit inspired so I wrote a sequential and parallel "forward pipe operators" for arrays:
let sleep ms = System.Threading.Thread.Sleep(millisecondsTimeout = ms)
let (|>-) a f = a |> Array.map(fun x -> f x)
let (|>=) a f = a |> Array.Parallel.map(fun x -> f x)
#time
[|0 .. 20|] |>- (fun x -> sleep 1000;x * x)
[|0 .. 20|] |>= (fun x -> sleep 1000;x * x)
With the following results
> Real: 00:00:21.009, CPU: 00:00:00.004, GC gen0: 0, gen1: 0
val it : int [] =
[|0; 1; 4; 9; 16; 25; 36; 49; 64; 81; 100; 121; 144; 169; 196; 225; 256; 289;
324; 361; 400|]
> Real: 00:00:06.005, CPU: 00:00:00.003, GC gen0: 0, gen1: 0
val it : int [] =
[|0; 1; 4; 9; 16; 25; 36; 49; 64; 81; 100; 121; 144; 169; 196; 225; 256; 289;
324; 361; 400|]
type CircuitElement =
| Wire
| Battery of float<V> // voltage of the battery
| Resistor of float<O> // resistance of the resistor
| Capacitor of float<F> // capacitance of the capacitor
| Series of CircuitElement * CircuitElement
| Parallel of CircuitElement * CircuitElement
static member (---) (c1, c2) = Series (c1,c2)
static member (|||) (c1, c2) = Parallel (c1,c2)
Global operators are always bit dangerous because of shadowing.
Very good points, I missed the locality of the operators :+1:
Hi Paul.
I can see that you define your overloaded operators as:
let (---) c1 c2 = Series (c1,c2)
let (|||) c1 c2 = Parallel (c1,c2)
I'm just thinking if "series" is
---
whouldn't it make sense that "parallel" should be===
?