newLISP Fan Club

Forum => newLISP newS => Topic started by: ale870 on August 21, 2008, 04:17:57 PM

Title: Best method to create fixed length strings
Post by: ale870 on August 21, 2008, 04:17:57 PM
Hello,



I need to make fixed length strings, starting from variable length strings.

Example:



"12" -> "12--" (4 characters in total)

"1" -> "1---"

"1234 -> "1234"



I have a solution but I think it is good for imperative programming, and it is not a good solution for functional languages.

Can you give me some suggestions and/or show me some small algorithms to make it in a "functional" fashion?



Thank you!
Title:
Post by: HPW on August 21, 2008, 10:35:52 PM
When your input are only numbers then how about:



>(replace " "(format "%-4s" "12")"-")
"12--"


For other input containing a space a regex would do it:



>(replace " +\z" (format "%-8s" "12 12") (dup "-" (length $0)) 0)
"12 12---"
Title: Re: Best method to create fixed length strings
Post by: DrDave on August 22, 2008, 01:25:30 AM
Here are the functions that can help you. I think you'll see how to make use of them to create a function that takes as parameters your input string and a filler string and returns a string of length 4.



Note that s1 and filler aren't restricted to just strings but can really be almost anything you want: numbers, strings, expressions.



(set 'filler "----") //assume max length of s1 is 4

(set 's1 "12")

(set 'new-string (string s1 filler) //concatenates s1 and filler into a string

(slice new-string 0 4)  //copies  first 4 characters as a string and leaves new-string

(0 4 new-string) //implicit SLICE



So, if s1 is the empty string, you'll get the filler string returned. Otherwise, you'll get your starting string padded with filler but truncated to the first 4 characters.
Title:
Post by: cormullion on August 22, 2008, 08:39:56 AM
Hi Alessandro! How about this:


(set 'buffer "")
  (for (i 0 3)
     (if (< i (length (string n)))
         (write-buffer buffer (nth i (string n)))
         (write-buffer buffer "-")))
  (println "result " buffer)


Ahh. Perhaps not. :)  HPW and DrDave have more newLISP-y ways, I think. Variations on a theme:


(0 4 (string n "----"))

(select (append (string n) "----")  (sequence 0 3)))

(join (array-list (array 4 (explode (string n (dup "-" 4))))))

Good fun for a Friday afternoon... :)
Title:
Post by: xytroxon on August 22, 2008, 11:16:53 AM

> (setq s1 12)
12
> (slice (string s1 "----") 0 4)
"12--"


Q.E.D. ;)



--xytroxon
Title:
Post by: DrDave on August 22, 2008, 12:11:03 PM
Uh, guys, he said he wanted it in a "functional" fashion, so I take that to mean he wants to have function to use. So now that we danced all around writing a function, here it is:


(define (make4 s1)
  (0 4 (string s1 "----")))

usage:
(make4 "ABc")  --> "ABc-"

(make4  (+ 3 4)) --> "7---"

(make4 (list 'a)) --> "(a)-"


As I said previously, s1 isn't restricted to being numeric, as he used in his original example, or a string, as we might be tempted to expect, but can be any valid expression. STRING takes care of all the "heavy lifting" of converting the result of s1 to a string and then concatenating it with the filler.
Title:
Post by: ale870 on August 22, 2008, 12:16:26 PM
Thank you!

Your fantasy is without limits! :-)



Some solutions seems more "traditional" (imperative programming) but other ones are really "functional".

I love the solution proposed by cormullion: (0 4 (string n "----"))



I think newLisp and functional languages are great to solve complex solutions in easy clever ways!



I always need your help since I'm a "imperative programmer" for a long time, and sometimes I find hard to "change my mind" to think in "functional" way!!!



THank you again, everybody helped me so much!!!
Title:
Post by: ale870 on August 22, 2008, 02:50:45 PM
Hello DrDave, your function is good, so I expanded it!


(define (make-n s1 argCount)
    (0 argCount (string s1 (dup "-" argCount) ) ) )


Usage:





> (make-n 123 5)
"123--"
> (make-n 123 5)
"123--"


:-)
Title:
Post by: DrDave on August 22, 2008, 10:31:04 PM
Alessandro,



Let's go another step towards more generic:


(define (make-n s1 argCount (filler "-"))
    (0 argCount (string s1 (dup filler argCount))))

usage:
>(make-n "ABc" 7)  --> "ABc----"

>(make-n "ABc" 7 "+")  --> "ABc++++"

>(make-n "AB78" 7 " ")  --> "AB78   "
Title:
Post by: ale870 on August 23, 2008, 06:27:32 AM
Yes! I like it! I will use it in my app!