concept operator in category .net

appears as: operator, operators, n operator, operator, The operator, operators
Concurrency in .NET

This is an excerpt from Manning's book Concurrency in .NET.

The order of computation of an aggregation that runs in parallel using PLINQ (or PSeq) applies the Reduce function differently than the sequential version. In Listing 5.8, the sequential result was computed in a different order than the parallel result, but the two outputs are guaranteed to be equal because the operator + (plus) used to update the centroid distances has the special properties of associativity and commutativity. This is the line of code used to find the nearest centroid:

Listing 10.29 AsyncResult conditional combinators
let gt (value:'a) (ar:AsyncResult<'a>) = asyncResult {  #1  
        let! result = ar
        return result > value
    }    

let doInvest stockId =
 let shouldIBuy =   #2  
        ((getStockIndex "^IXIC" |> gt 6200.0)
 <|||>   #3  
            (getStockIndex "^NYA" |> gt 11700.0 ))
 <&&&> ((analyzeHistoricalTrend stockId) |> gt 10.0)   #4  
 |> AsyncResult.defaultValue false   #5  

 let buy amount = async {   #6  
        let! price = getCurrentPrice stockId
        let! result = withdraw (price*float(amount))
        return result |> Result.bimap (fun x -> if x then amount else 0) 
 (fun _ -> 0)   #7  
        }

 AsyncComb.ifAsync shouldIBuy   #8  

 (buy <!> (howMuchToBuy stockId))   #9  

 (Async.retn <| Error(Exception("Do not do it now")))   #10  

 |> AsyncResult.handler   #11  

#1   Checks if a given value is greater than the result of an asynchronous operation returning an AsyncResult type. The generic type 'a must be comparable.
#2   Applies the logical async OR operator to evaluate the given functions. This function represents a predicate that says whether or not you should buy based on the current market.
#3   Exploits the async infix OR operator
#4   Applies the logical async infix AND operator to evaluate the given functions
#5   Helper function that returns the default value of a given type, lifting the output into the AsyncResult type. This function is used in case of error during the calculation process.
#6   Checks the current bank balance returning the amount of stocks that can be purchased
#7   Verifies if the transaction is successful and then either returns Async<int>, wrapping the amount value if the transaction is successful, or returns Async<int> zero
#8   Runs a conditional If statement to decide to buy or not buy the given stock
#9   If the shouldIBuy operation is positive, the buy function is lifted (AsyncResult) and executed against the amount of the number of stocks recommended to buy. This amount is the output of the function howMuchToBuy.
#10   If the shouldIBuy operation is negative, an error message is displayed.
#11   Wraps the overall function combinators in an async error catch


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":41},{\"line\":0,\"ch\":52}],[{\"line\":8,\"ch\":1},{\"line\":8,\"ch\":6}],[{\"line\":10,\"ch\":1},{\"line\":10,\"ch\":6}]]"}
!@%STYLE%@!

In the last line of code, the composition (>>) operator lets you eliminate the explicit need for an input parameter definition. The F# compiler understands that the function plusOneIsEven is expecting an integer as input. The kind of function that doesn’t need parameter definitions is called a point-free function.

C# in Depth

This is an excerpt from Manning's book C# in Depth.

C# 2 provides the default value expression to care for just this need. The specification doesn’t refer to it as an operator, but you can think of it as being similar to the typeof operator, just returning a different value. The following listing shows this in a generic method, and also gives an example of type inference and a conversion type constraint in action.

If you need a way of basing one value on another, follow the lead of DateTime and TimeSpan—provide methods and operators that return a new value rather than modifying an existing one. This avoids all kinds of subtle bugs, including situations where you may appear to be changing something, but you’re actually changing a copy. Just say No to mutable value types.

The executive summary is that if there’s an operator or conversion available on a non-nullable value type, and that operator or conversion only involves other non-nullable value types, then the nullable value type also has the same operator or conversion available, usually converting the non-nullable value types into their nullable equivalents. To give a more concrete example, there’s an implicit conversion from int to long, and that means there’s also an implicit conversion from int? to long? that behaves in the obvious manner.

Unfortunately, although that broad description gives the right general idea, the exact rules are slightly more complicated. Each rule is simple, but there are quite a few of them. It’s worth knowing about them because otherwise you might end up staring at a compiler error or warning for a while, wondering why it believes you’re trying to make a conversion that you never intended in the first place. We’ll start with the conversions and then look at the operators.

IronPython in Action

This is an excerpt from Manning's book IronPython in Action.

Python follows the normal BODMAS[12] precedence rules for operators in expressions. The best reference I could find on Python operators is from A Byte of Python by CH Swaroop at http://www.swaroopch.com/notes/Python_en:Operators_and_Expressions#Operator_Precedence.

Enums can be combined as flags with the bitwise-or operator (|) if you annotate them with the Flags attribute, in which case you should also specify the value of each member (by default, they’re assigned sequentially from 0). For example, if you wanted the RobotColor values to be combinable, you could define it as follows: