Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

step-tuple

step-tuple: func [
    val [tuple!]
    /by step [tuple!]
][
    if any [
        3 <> length? val ; can only step values of 3 segments
        val = 255.255.255
    ] [make error! "Can't step tuple value"]
    either step [
        either (val + step) <> val [val + step] [
            make error! "Can't step tuple value"
        ]
    ][
        steps: [0.0.1 0.1.0 1.0.0]
        for i 3 1 -1 [
            either val/:i = 255 [val/:i: 0] [
                return val + first steps
            ]
            steps: next steps
        ]
    ]
]
; foreach val [
;     0.0.0 0.0.255
;     0.1.0 0.255.0 0.255.255
;     1.0.0 0.255.255
;     254.255.255 255.255.254
;     255.254.255 254.255.255
; ][
;     print [val tab step-tuple val]
; ]
; print attempt [step-tuple 255.255.255]
; print attempt [step-tuple 255.255.255.0]

step-version

step-version: func [
    val [tuple!]
    segment [word!] "'major, 'minor, or 'build"
    /local res
][
    switch segment [
        build [res: val + 0.0.1]
        minor [res: val + 0.1.0  res/3: 0]
        major [res: val + 1.0.0  res/2: 0  res/3: 0]
    ]
    either res > val [res] [none]
]

can-step-version?: func [
    val [tuple!]
    segment [word!] "'major, 'minor, or 'build"
][
    either step-version val segment [true] [false]
]

; foreach val [
;     0.0.0 0.0.255
;     0.1.0 0.255.0 0.255.255
;     1.0.0 0.255.255
;     254.255.255 255.255.254
;     255.254.255 254.255.255
; ][
;     foreach seg [build minor major] [
;         print [val tab seg tab can-step-version? val seg tab attempt [step-version val seg]]
;     ]
; ]
; print attempt [step-version 254.255.255 'test]
; halt

convert list of list/tuple to list of object named, python

my version of "named tuple"
class OList:
    """
        l=[("martin","jean"),("dupond","yves"),("klein","michel")]
        or
        l=[["martin","jean"],["dupond","yves"],["klein","michel"]]

        ll = OList("nom","prenom")(l)
        print ll[2].prenom,"==",ll[2][1],"==",l[2][1]
    """
    def __init__(self,*a):
        self.__n=list(a)
    def __call__(self,liste):
        class NList(list):
            def __init__(self,n,l):
                assert len(l)==len(n), "No match "+str(n)+" and "+str(l)
                list.__init__(self,l)
                for i in n:
                    setattr(self,i,l[ n.index(i) ])
                self.__noms = n
            def __repr__(self):
                m=" ".join(["""%s='%s'""" % (i,str(getattr(self,i))) for i in self.__noms])
                return "<objet %s>" % m
        return [ NList(self.__n, i) for i in liste ]
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS