Skip to content
Anssi Halmeaho edited this page Apr 1, 2021 · 1 revision

stdpp

Provides a capability to "pretty-print" FunL data structures so those are more readable.

Composite data structures (lists and maps) are formatted by using indentation to show nesting in easier (to read) way.

Services

form

Formats value (1st arguments) as pretty-printed as returns it as string. There can be optionally 2nd argument given to specify string which is used for indentation (default is tab).

Note. if value contains only some of following data types then output string can be evaluated by eval:

  • map
  • list
  • string
  • int
  • bool
  • float

type: function

Format:

call(stdpp.form <value> (optional: indentation-string)) -> string

pform

Similar to form but can be called also from procedure.

Also allows opaque values be inside value to be formatted.

type: procedure

Format:

call(stdpp.pform <value> (optional: indentation-string)) -> string

pprint

Formats given value to pretty-printed format and writes result to standard output.

Prints newline at the end.

Returns same value which was given as argument.

type: procedure

call(stdpp.pprint <value>) -> <value>

Examples

Example: Using stdpp.form

ns main

import stdpp

main = func()
	v = map(
		0.25 'quarter'
		10 'ten'
		list('some' 'key') list('some other map' map(1 2 3 4))
	)

	call(stdpp.form v)
end

endns

Output is:

'
map(
        10
        'ten'
        list(
                'some'
                'key'
        )
        list(
                'some other map'
                map(
                        1
                        2
                        3
                        4
                )
        )
        0.25
        'quarter'
)'

Example: Using stdpp.pform

ns main

import stdpp

main = proc()
	import stdvar

	v = list(
		'some'
		'text'
		50
		list(map('key-1' 'value-1' 'key-2' 'value-2' 'another-map' map(1 list('value'))))
		func() 'dummy value' end
		call(stdvar.new 'this is opaque value')
	)

	call(stdpp.pform v)
end

endns

Output is:

'
list(
        'some'
        'text'
        50
        list(
                map(
                        'key-2'
                        'value-2'
                        'key-1'
                        'value-1'
                        'another-map'
                        map(
                                1
                                list(
                                        'value'
                                )
                        )
                )
        )
        func-value: file: ../pp_example2.fnl line: 14 pos: 7
        opaque(var-ref('this is opaque value'))
)'

Example: Using stdpp.pprint

ns main

import stdpp

main = proc()
	import stdvar

	v = list(
		list(map('key-1' 'value-1' 'key-2' 'value-2' 'another-map' map(1 list('value'))))
		func() 'dummy value' end
		call(stdvar.new 'this is opaque value')
	)

	call(stdpp.pprint v)
end

endns

Output is:

list(
        list(
                map(
                        'key-2'
                        'value-2'
                        'key-1'
                        'value-1'
                        'another-map'
                        map(
                                1
                                list(
                                        'value'
                                )
                        )
                )
        )
        func-value: file: ../pp_example3.fnl line: 11 pos: 7
        opaque(var-ref('this is opaque value'))
)
list(list(map('key-2' : 'value-2', 'key-1' : 'value-1', 'another-map' : map(1 : list('value')))), func-value: file: .
./pp_example3.fnl line: 11 pos: 7, opaque(var-ref('this is opaque value')))

Example: Giving alternate indentation string

ns main

import stdpp

main = func()
	v = map(
		0.25 'quarter'
		10 'ten'
		list('some' 'key') list('some other map' map(1 2 3 4))
	)

	call(stdpp.form v '....')
end

endns

Output is:

'
map(
....10
....'ten'
....list(
........'some'
........'key'
....)
....list(
........'some other map'
........map(
............1
............2
............3
............4
........)
....)
....0.25
....'quarter'
)'