Formatter at cursive IDE - intellij-idea

Is there any way to pretty format code in cursive?
For example I have inline code:
(reduce (fn [acc [key value]] (apply str acc (repeat (read-string key) value))) "" (apply assoc {} data))
Is there any way to formatting in cursive in new lines with ⌥⌘L ? Any Advice, guides articles would help. Thx!

Related

Is there any way to mock a function in clojure?

Is there any way to mock a function in clojure. For example I have this function: (defn a [x] (utils/count x)) I need to write a test, but I do not know how to mock the utils/count (for example). what if I have several functions inside the same function and I need to test it? (defn a [x] (utils/count x) (utils/count2 x) (test/other x))
You can use with-redefs e.g.
(deftest t
(with-redefs [utils/count (fn [x] 2)
utils/count2 (fn [x] 3)
test/other (fn [x y] :result)]
(let [result (a 2)])))
See also https://ask.clojure.org/index.php/9077/how-do-you-mock-in-clojure which suggests organizing your code so you don't need mocks, where possible.

functional programming : understand parser combinator

I was trying to solve a problem using a parser combinator. I tried the following:
Note: the below code uses the combinator library
styleParserItalic : Bool -> Parser ( List (List Char , Style))
styleParserItalic bolded =
let
style = if bolded then Italic else Unstyled
in
(end `andThen` always ( succeed ( [] )))
<|> (string "(!ITALIC!)" `andThen` \_ -> styleParserItalic ( not bolded ) )
<|> ( anyChar `andThen` \c -> styleParserItalic bolded `andThen` \cs -> succeed ((c :: [],style) :: cs) )
I am struggling to understand how this parser runs since the styleParserItalic parser is called before the parser succeeds.
Could someone explain how the parser works when it is given a string of characters?
If someone is interested in the purpose of the parser and the full code, here is my previous question.
Here is what I have understood thus far
The parser will check first if it is the end of a line , if not it will try to parser the string (!ITALIC!) if that the case then it will call the parser with with parameter True or false (if false then will makes it true ..)
If the parser does not find the the string (!ITALIC!) it will try to parse any character then it will call the parser again.
What confuses me is that the parser will keep calling itself as long as it succeeds with parsing any character!
edit :* NOTE THE BELOW IS NOT A PART OF THE QUESTION, JUST TO SHARE THE CODE IF SOMEONE IS INTERESTED
thanks for all responses, I have updated the parser to parse Bold italic underline..., as per the below screen shot
type Style = Bold| Unstyled | Italic | Coded | Lined | Titled | Marked | Underline
styleParser : Bool ->Bool ->Bool ->Bool-> Bool-> Bool->Bool
-> Parser ( List (List Char , (Style,Style,Style,Style,Style,Style,Style)))
--(bold,italic ,code,line ,Titled,mark)
styleParser bolded italiced coded lined titled marked underlined=
let
style = (
if bolded then Bold else Unstyled
,if italiced then Italic else Unstyled
,if coded then Coded else Unstyled
,if lined then Lined else Unstyled
,if titled then Titled else Unstyled
,if marked then Marked else Unstyled
,if underlined then Underline else Unstyled
)
in
(end `andThen` always ( succeed ( [] )))
<|> (string "//" `andThen` \_ -> styleParser bolded italiced coded lined titled marked (not underlined))
<|> (string "**" `andThen` \_ -> styleParser (not bolded) italiced coded lined titled marked underlined)
<|> (string "*" `andThen` \_ -> styleParser bolded (not italiced) coded lined titled marked underlined)
<|> (string "`" `andThen` \_ -> styleParser bolded italiced (not coded) lined titled marked underlined)
<|> (string "/br" `andThen` \_ -> styleParser bolded italiced coded (not lined) titled marked underlined)
<|> (string "/*" `andThen` \_ -> styleParser bolded italiced coded lined (not titled) marked underlined)
<|> (string "{-" `andThen` \_ -> styleParser bolded italiced coded lined titled (not marked) underlined)
<|> ( anyChar `andThen` \c -> styleParser bolded italiced coded lined titled marked underlined `andThen` \cs -> succeed ((c :: [],style) :: cs) )
foldStyleHtml : List ( List Char , ( Style,Style,Style,Style,Style,Style,Style) ) -> List (Html Msg)
foldStyleHtml lst =
List.map styleToHtml lst
styleToHtml : ( List Char, (Style ,Style,Style,Style,Style,Style,Style)) -> Html Msg
styleToHtml (a,b) =
case b of
(Bold,Italic,_,_,_,_,Unstyled) -> strong [] [em [][ text (String.fromList a)]]
(Bold,Italic,_,_,_,_,Underline) -> u[][ strong [] [em [][ text (String.fromList a)]]]
(Bold,Unstyled,_,_,_,_,Underline) -> u[][ strong [] [text (String.fromList a)]]
(Unstyled,Italic,_,_,_,_,Underline) -> u[][ em [] [text (String.fromList a)]]
(Unstyled,Italic,_,_,_,_,_) -> em[] [text (String.fromList a)]
(Bold,Unstyled,_,_,_,_,_) -> strong [][ text (String.fromList a)]
(_,_,Coded,_,_,_,_) -> code [codeStyle ][text (String.fromList a)]
(_,_,_,Lined,_,_,_) -> br [][text " "]
-- (_,_,_,_,Titled,_,_) -> div [][text (String.fromList a)]
(_,_,_,_,_,Marked,_) -> mark [][text (String.fromList a)]
(_,_,_,_,_,_,Underline) -> u [][text (String.fromList a)]
(_,_,_,_,_,_,_) -> text (String.fromList a)
htmlParser : Parser (List (Html Msg))
htmlParser =
styleParser False False False False False False False `andThen` (succeed << foldStyleHtml )
runParser : Parser (List (Html Msg)) -> String -> Html Msg
runParser parser str =
case parse parser str of
(Ok htmls,_)-> div [] htmls
(Err err, _) -> div [ style [("color", "red")] ] [ text <| toString <| err]
Parser combinators (generally) consume input as they succeed. In this library, if string "(!ITALIC!)" fails, it will not consume any input. Since the <|> combinator is used, it then tries to use the next part of the code that starts with anyChar.
When anyChar succeeds, it consumes that single character and captures it in c after andThen. Then the remaining string (everything but the character captured by anyChar) is then "crawled" when the recursive call to styleParserItalic bolded is made. That second andThen captures the output of the recursive combinator into cs and prepends the captured character onto the rest of the list of characters from the recursive call.
I think the important part to remember is that the combinators consume input as they succeed and (generally) don't consume input when they fail.
First a few simplications...
(1) Every element of the inner List in the signature:
styleParserItalic : Bool -> Parser ( List (List Char , Style))
^^^^^^^^^
is just a single character. By just removing :: [] from the last line,
<|> ... `andThen` \cs -> succeed ((c ,style) :: cs) )
^^^
removed `:: []`
you can make it have this signature.
(2) Note that the bolded argument only affects the Style - it has not effect on control flow. It really should be called italic since the style appearing in the output is Italic if the argument is True and Unstyled otherwise.
Also note that once this parameter is set the True, it will remain True in
all subsequent recursive calls.
So now the algorithm is:
If at the end of the line, return the empty list.
If at (!ITALIC!), use Italic for the style in the remainder of the parse.
Otherwise, parse a character, parse the remainder of the line and concatenate the results.
An approximate Python algorithm would be something like:
def parseLine(style, line):
result = []
while line:
if line.startsWith('(!ITALIC!)'):
line = line[8:]
style = Italic
# loop around
else:
result.append( (line[0], style) )
line = line[1:]
return result

CLOS: Format initialization argument list for make-instance

I've been scratching my head on this for a while now - maybe someone could shed some light on how to format an initialization argument list for 'make-instance' from a nested list containing (key value) sublists. Example:
(make-instance 'myclass :initarg1 1 :initarg2 2 :initarg3 '(a b))
If I have the keywords and values in a list like so:
'((initarg1 1) (initarg2 2) (initarg3 '(a b)))
Any help and pointers appreciated!
Thanks,
Marleynoe
(apply #'make-instance 'myclass
(loop for (parameter value) in '((initarg1 1) (initarg2 2) (initarg3 '(a b)))
collect (intern (symbol-name parameter) (find-package :keyword))
collect value))
(apply #'make-instance 'myclass
(mapcan #'(lambda (param)
(list (intern (symbol-name (car param)) (find-package :keyword))
(cadr param)))
'((initarg1 1) (initarg2 2) (initarg3 '(a b)))))
The idea is that each (initargk k) parameter pair is mapped to a fresh list (:initargk k) and then all of them are concatenated in order together. This is a typical pattern for the map function mapcan.

Clojure way of reading large files and transforming data therein

I am processing a Subrip subtitles file which is quite large and need to process it one subtitle at a time. In Java, to extract the subtitles from file, I would write a method with following signature:
Iterator<Subtitle> fromSubrip(final Iterator<String> lines);
The use of Iterator gives me two benefits:
The file is never in the memory in its entirety, nor is any of its transformed stage.
An abstraction wherein I can loop over a collection of Subtitle objects without the memory overhead.
Since iterators are by nature imperative and mutable, they're probably not idiomatic in Clojure. So what is the Clojure way to deal with this sort of situation?
As Vladimir said, you need to handle the laziness and file closing correctly. Here's how I did it, as shown in "Read a very large text file into a list in clojure":
(defn lazy-file-lines
"open a (probably large) file and make it a available as a lazy seq of lines"
[filename]
(letfn [(helper [rdr]
(lazy-seq
(if-let [line (.readLine rdr)]
(cons line (helper rdr))
(do (.close rdr) nil))))]
(helper (clojure.java.io/reader filename))))
read all files from a directory, a lazy way.
using go black and channel.
code:
(ns user
(:require [clojure.core.async :as async :refer :all
:exclude [map into reduce merge partition partition-by take]]))
(defn read-dir [dir]
(let [directory (clojure.java.io/file dir)
files (filter #(.isFile %) (file-seq directory))
ch (chan)]
(go
(doseq [file files]
(with-open [rdr (clojure.java.io/reader file)]
(doseq [line (line-seq rdr)]
(>! ch line))))
(close! ch))
ch))
invoke:
(def aa "D:\\Users\\input")
(let [ch (read-dir aa)]
(loop []
(when-let [line (<!! ch )]
(println line)
(recur))))
================
reify the Iterable interace, can be used in java.
MyFiles.clj:
(ns user
(:gen-class :methods [#^{:static true} [readDir [String] Iterable]])
(:require [clojure.core.async :as async :refer :all
:exclude [map into reduce merge partition partition-by take]]))
(defn -readDir [dir]
(def i nil)
(let [ch (read-dir dir)
it (reify java.util.Iterator
(hasNext [this] (alter-var-root #'i (fn [_] (<!! ch))) (not (nil? i)))
(next [this] i))
itab (reify Iterable
(iterator [this] it))]
itab))
java code:
for (Object line : MyFiles.readDir("/dir")) {
println(line)
}
You can use lazy sequences for this, for example, line-seq.
You must be careful, however, that the sequence returned by line-seq (and other functions which return lazy sequences based on some external resource) never would leak out of e.g. with-open scope because after the source is closed, further reading from lazy sequence will cause exceptions.

Is clojure's read file structure, i.e. with-open and clojure.java.io/reader, efficient enough for frequent access?

Suppose I write a function to parse data from a txt file with with-open and clojure.java.io/reader, and then I wrote another function to call the reader function multiple of times in order to process data, e.g.
(defn grabDataFromFile [file patternString]
(let [data (atom [])]
(with-open [rdr (clojure.java.io/reader file)]
(doseq [line (line-seq rdr)]
(if (re-matches (re-pattern patternString) line) (swap! data conj line))))
#data))
(defn myCalculation [file ]
(let [data1 (grabDataFromFile file "pattern1")
data2 (grabDataFromFile file "pattern2")
data3 (grabDataFromFile file "pattern3")]
;calculations or processes of data1, data2, data3....))
My question is, inside this myCalculation function, is the underlying code smart enough to open the file just once with clojure reader, and get all data needed in one shot? Or does it open and close the file as many times as number of calls for function grabDataFromFile ? ( In this example, 3)
A follow up question would be, what can I do to speed up if the reader is not smart enough, and if I have to intentionally separate "parser" code with "processing" code?
grabDataFromFile will open and close reader (on exit) every time it is called. The underlying code cannot be that smart such that a function can detect the context of its caller without some explicitly provided information.
Make grabDataFromFile to accept another function which is your parser logic that operates on each line (or it could be any function that you want to perform on each line)
(defn grabDataFromFile [file patternString process-fn]
(with-open [rdr (clojure.java.io/reader file)]
(doseq [line (line-seq rdr)]
(process-fn line))))
(defn myCalculation [file]
(let [patterns [["pattern1" (atom [])]
["pattern2" (atom [])]
["pattern3" (atom [])]]
pattern-fns (map (fn [[p data]]
(fn [line]
(if (re-matches (re-pattern p) line)
(swap! data conj line)))) patterns)
pattern-fn (apply juxt pattern-fns)]
(grabDataFromFile file pattern-fn)
;perform calc on patterns atoms
))