All posts by sqlxprt

About sqlxprt

sqlexpert ! Interested in Haskell Certified in Scala

Decode DAX from Power BI Desktop pbix file

Advertisements

Monitor Bitlocker EncryptionPercentage

was briefly tempted, at last night’s F# meetup, to mention that I converted a 1 line Powershell into a 20 line F# script, but about 90% ashamed (why?!)  I later graphed it using Graph.js and Giraffe (F#) running under Kestrel, but that would be for a separate post. Most posts about this use VBS or Powershell; it was hard to find good C# examples of reading from WMI and using InvokeMethod (but there is a link in the gist to a good one.)

Note how the 6 arguments to GetConversionStatus are pasted as an array of objects.

Dynamic creation of an Excel Unprotect application using Powershell

Scenario: you want to share an application with random users on the web, or within your organization, but you want to allay fears that your Excel workbook may be malicious.

Solution: distribute it as a Powershell script that creates the Excel workbook programmatically. Powershell may not be easy to read, but it reads linearly top-to-bottom and is easier to review than by looking at all the objects with an xlsm archive (provided that your application is very small.)

Specific use-case: There is an application called “Excel Unlocker.xlsm” out on the web, which seems very useful, but I wish not to run it due to the difficulty of being 100% sure there isn’t any hidden code in there. Considering it has activeX controls as well as VBA macros, I think my suspicion may not be wholly unwarranted (in general), although I haven’t found any suspicious code in the spreadsheet yet. Nevertheless, I rebuilt the workbook from scratch without running any of its macros, and it is functional, but how do I distribute my new rebuilt workbook to other cautious people (like me), who may wish to run this on their files (workbooks that are encrypted or protected, or simply have VBProject protection enabled with an unknown password?) Answer: re-build the workbook programmatically, using Powershell.

[Hours later…]

Okay, it took too long, but here it is. (Hey WordPress; nice paste of my gist! Thanks!) :

 

Haskell interlude

Okay, wordpress sux majorly; it kept messing this up every time I tried to post.

I’ll just have to write a successor to WordPress in Haskell (starting with clckwrks?) … sigh… (if I have time to rewrite every major piece of software in the world…)

I’ll probably post some SQL, PowerShell, or even maybe DAX soon (when I get deeper into DAX). Or at least F#. But here’s some Haskell that shows how concisely a binary-file-decoding task can be concisely-written.

The story: Years ago, I write a sort of Scrabble-cheat program called anagram ( http://my.streetpulse.com/shared/anagram.zip ) . It does a brute-force search of a word list to find all possible anagrams of the given rackletters, including blanks (repeatedly, due to careless coding, and to avoid having to implement a sort, or bucketization, or memory-allocation.) To save RAM, I encoded the word-list enable1.txt with what I called “prefix-compression”, meaning that if you have aardvark first, then aaple next, the encoded file just stores “ple” for the second word, with a byte-sized integer 2 to indicate that two characters can be copied from the previous word. This encoding can be decoded with scanl as-follows:
<pre>I’ll probably post some SQL, PowerShell, or even maybe DAX soon (when I get deeper into DAX). Or at least F#. But here’s some Haskell that shows how concisely a binary-file-decoding task can be concisely-written.

The story: Years ago, I write a sort of Scrabble-cheat program called anagram ( http://my.streetpulse.com/shared/anagram.zip ) . It does a brute-force search of a word list to find all possible anagrams of the given rackletters, including blanks (repeatedly, due to careless coding, and to avoid having to implement a sort, or bucketization, or memory-allocation.) To save RAM, I encoded the word-list enable1.txt with what I called “prefix-compression”, meaning that if you have aardvark first, then aaple next, the encoded file just stores “ple” for the second word, with a byte-sized integer 2 to indicate that two characters can be copied from the previous word. This encoding can be decoded with scanl as-follows:</pre>
<pre>I’ll probably post some SQL, PowerShell, or even maybe DAX soon (when I get deeper into DAX). Or at least F#. But here’s some Haskell that shows how concisely a binary-file-decoding task can be concisely-written.

The story: Years ago, I write a sort of Scrabble-cheat program called anagram ( http://my.streetpulse.com/shared/anagram.zip ) . It does a brute-force search of a word list to find all possible anagrams of the given rackletters, including blanks (repeatedly, due to careless coding, and to avoid having to implement a sort, or bucketization, or memory-allocation.) To save RAM, I encoded the word-list enable1.txt with what I called “prefix-compression”, meaning that if you have aardvark first, then aaple next, the encoded file just stores “ple” for the second word, with a byte-sized integer 2 to indicate that two characters can be copied from the previous word. This encoding can be decoded with scanl as-follows:

I tried <pre> and <blockquote> in html editing mode but couldn’t get this indented code to save properly; WordPress cuts it off every time.  Maybe I’ll post it to github.com/tallpeak … ahh, that’s nice; here it is:

https://gist.github.com/tallpeak/24dabfce6b5bf901d0e31eab0ca30aef

Why functional-programming?

Why am I fascinated with functional-programming, denotational-semantics, denotational-design, type-driven-design, etc?

Is it just because I’m more interested in more-reliable software? No.

Is it because I want easier-to-read, faster-to-change, more-flexible software? Almost, but not quite.

Rather, I feel that it’s because I want to preserve *meaning* in software, because that is the only thing of value. And I define meaning as relationships defined within a context. For a function, the meaning is the relationship between input and output, and the context is the type of the function.

Only by preserving “meaning” have we preserved the specification of the program, in a fine-enough grain to be reusable.

Haskell seems to me to be the language that best enables writing meanings of aspects of a program down in a concise, specification-oriented notation, in immutable rules about the system. Writing these meaning-relations as invariant-constraints (which I just called “immutable rules”) enables building systems that do not need to change radically when some small aspect of the environment changes.

This seems like a very important step for software to take, if we want to stop spinning our wheels in the IT world, reinventing the wheel thousands or millions of times, and move up the layers of abstraction toward truly flexible, and perhaps even slightly “intelligent” software.

Demos from StrangeLoop of live-programming environments seem quite relevant to this process of implementing abstractions in a functional-programming-language such as Haskell, because such demos seem like they would be in some cases almost trivial to implement with a model such as FRP. (A possible example is https://www.youtube.com/watch?v=i3Xack9ufYk  … but the video about Eve by Chris Granger may be a better example.)

 

 

 

x ^ n :: Double -> Int -> Double

Here’s a Haskell implementation of a power function, x ^ n, where x is a double and n is an int.

 

import Text.Printf 
import Data.Char

toBits :: Int -> [Int]
toBits x= reverse (map (\x->ord x - 48) (printf "%b" x) )

pown x n = 
	product $ 
	filter (>0) $ 
	zipWith (*) 
		(map toInteger$toBits n) 
		(iterate (\x->x*x) x)

main = print $ pown 2 14