billstclair/elm-bitwise-infix/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

BitwiseInfix

This module provides infix versions of Elm's Bitwise module functions. This greatly eases conversion of existing JavaScript code. Intead of changing infix to prefix, just prepend a twiddle (~) to the operators.

Bitwise.not has no infix operator, since Elm doesn't provide single argument "infix" operators. So I named it BitwiseInfix.lognot, in honor of its Common Lisp name.

It is intended to be imported exposing (..), so that the operators can appear unadorned in your code.

All the operators are left associative.

I mirrored the JavaScript precedences, putting the shift operators at lower precedence than arithmetic, but higher than comparison, and the others lower than comparison.

(~<<), (~>>), and (~>>>) have a precedence of 5.
(~&) has a precedence of 3.
(~^) has a precedence of 2.
(~|) has a precedence of 1.

Examples:

import BitwiseInfix exposing (..)

3 ~& 1 ~| 4 ~& 12     -- 5
3 ~& (1 ~| 4) ~& 12   -- 0
2 ~<< 1 ~| 2          -- 6
2 ~<< (1 ~| 2)        -- 16
1 ~| 2 ~^ 2 ~| 1      -- 1
(1 ~| 2) ~^ (2 ~| 1)  -- 0
3 ~& 1 ~^ 4 ~& 12     -- 5
3 ~& (1 ~^ 4) ~& 12   -- 0
9 ~>> 1 ~<< 1         -- 8
9 ~>> (1 ~<< 1)       -- 2

Functions

(~&) : Int -> Int -> Int

Infix for Bitwise.and

(~|) : Int -> Int -> Int

Infix for Bitwise.or

(~^) : Int -> Int -> Int

Infix for Bitwise.xor

lognot : Int -> Int

Alias for Bitwise.not. Elm does not provide "infix" operators with only a single argument. This name let's you import BitwiseInfix exposing (..), without worrying about name collisions.

(~<<) : Int -> Int -> Int

Infix for Bitwise.shiftLeft

(~>>) : Int -> Int -> Int

Infix for Bitwise.shiftRight

(~>>>) : Int -> Int -> Int

Infix for Bitwise.shiftRightLogical