orlandpm / fsharp-circuits

showing how to model, draw, and calculate on DC circuits using F# features
11 stars 0 forks source link

Suggestion for a more intuitive operator for "parallel" #1

Open ghost opened 10 years ago

ghost commented 10 years ago

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 ===?

orlandpm commented 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!

ghost commented 10 years ago

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|]
dmitry-a-morozov commented 10 years ago
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.

ghost commented 10 years ago

Very good points, I missed the locality of the operators :+1: