Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functional style #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Functional style #2

wants to merge 5 commits into from

Conversation

hg42
Copy link

@hg42 hg42 commented Feb 1, 2014

changes to allow functional programming style.

example:

l       = 100.0
r       =  10.0
br      =  30.0

rod     = Cylinder(r=r, h=l)
ball    = Sphere(r=br)

parts = (
    rod.rotate(90,  0,  0).color([1, 0, 0])
  + rod.rotate( 0, 90,  0).color([0, 1, 0])
  + rod.rotate( 0,  0, 90).color([0, 0, 1])
  + ball.scale(r/br)
  + ~ball.scale(0.5).translate(l, 0, 0)
  + -ball.scale(0.5).translate(0, l, 0)
  + +ball.scale(0.5).translate(0, 0, l)
  )

To make this possible, each modifying method has to return a modified copy of itself.
This way an transformation doesn't change the receiving object and returns the transformed copy of the object instead. Otherwise variables would be changed by transformations.

As an alternative you could also use functions instead of variables to define a part, but the variables look more natural to me.
This style would look like this:
rod = lambda: Cylinder(r=r, h=l)
ball = lambda: Sphere(r=br)
...

  • ball().scale(r/br)

Instead I would like to reserve functions for combined operations or complex or parametric constructions.

Additionally I added unary operators for 'disable' = '-' = '*', 'background' = '+' = '%' and 'debug' = '~' = '#'.
Unfortunately there are only three (single character) unary operators in python, while openscad having four modifiers, so 'root' has no corresponding unary operator, but I think it's already cleaner to use variables for this.

Btw. for functional style it would be more convenient to use methods instead of assignments (e.g. obj.debug() instead of obj.debug = True, however everyone will use the unary operators, anyway.

hg42 added 5 commits February 1, 2014 15:59
--HG--
extra : source : 3f4e5502a0d7b061d630ccaeee704000cf8011fb
…background, unfortunately there is no more unary operator for root

--HG--
extra : source : f4f41ca4b6fdca4f8b1095e1ca91cc30e8271856
… without using a variable

--HG--
extra : source : f5c4c8ec33a51e9f24ec7132563a1759c971c05a
…ns a transformed copy of itself, so a=...; b=a.mod1(); c=a.mod2() works as expected

--HG--
extra : source : 28b912c6a6a4b4ba0510c06e1ed39982679e809f
…a mix of single objects or lists

--HG--
extra : source : 852e2467eb497c77a5303496748d7a0d93bf6be7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant