是否有可能在Elm中迭代联合类型?

时间:2016-06-30 20:06:37

标签: iteration elm

我想要向用户呈现颜色的类型联合。是否可以迭代所有类型的联合值?

pixmap.drawPixmap(fontPixmap, x_place, (TILE_HEIGHT - aGlyph.height) / 2,
aGlyph.srcX, aGlyph.srcY, aGlyph.width, aGlyph.height);

The only way (I have found) of drawing raster fonts (`.ttf`) is as follows:

    Example framework:
    ==================
    package com.badlogic.gdx.tests.bullet;

    /**
    Question       :   libgdx write text on texture
    interpreted as :   In libgdx, how to create dynamic texture?
    Answer         :   Use a private render function to draw in a private frame buffer and convert the frame buffer to Pixmap, create Texture.
    Author  :   Jon Goodwin
    **/

    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.Pixmap;
    ...//(ctrl-shift-o) to auto-load imports in Eclipse


    public class BaseBulletTest extends BulletTest
    {
    //class variables
    =================
    public Texture           texture     = null;//create this
    public Array<Disposable> disposables = new Array<Disposable>();
    public Pixmap            pm          = null;
    //---------------------------
        @Override
        public void create ()
        {
            init();
        }
    //---------------------------
        public static void init ()
        {
            if(texture == null) texture(Color.BLUE, Color.WHITE);
            TextureAttribute ta_tex     = TextureAttribute.createDiffuse(texture);
            final Material material_box = new Material(ta_tex, ColorAttribute.createSpecular(1, 1, 1, 1),
                                                       FloatAttribute.createShininess(8f));
            final long attributes1      = Usage.Position | Usage.Normal | Usage.TextureCoordinates;
            final Model boxModel = modelBuilder.createBox(1f, 1f, 1f, material_box, attributes1);
            ...
        }
    //---------------------------
        public Texture texture(Color fg_color, Color bg_color)
        {
            Pixmap pm = render( fg_color, bg_color );
            texture = new Texture(pm);//***here's your new dynamic texture***
            disposables.add(texture);//store the texture
        }
    //---------------------------
        public Pixmap render(Color fg_color, Color bg_color)
        {
            int width = Gdx.graphics.getWidth();
            int height = Gdx.graphics.getHeight();

            SpriteBatch spriteBatch = new SpriteBatch();

            m_fbo = new FrameBuffer(Format.RGB565, (int)(width * m_fboScaler), (int)(height * m_fboScaler), false);
            m_fbo.begin();
            Gdx.gl.glClearColor(bg_color.r, bg_color.g, bg_color.b, bg_color.a);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            Matrix4 normalProjection = new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(),  Gdx.graphics.getHeight());
            spriteBatch.setProjectionMatrix(normalProjection);

            spriteBatch.begin();
            spriteBatch.setColor(fg_color);
            //do some drawing ***here's where you draw your dynamic texture***
            font.draw(spriteBatch, "5\n6\n2016",  width/4, height - 20);//multi-line draw
            ...
            spriteBatch.end();//finish write to buffer

            pm = ScreenUtils.getFrameBufferPixmap(0, 0, (int) width, (int) height);//write frame buffer to Pixmap

            m_fbo.end();
    //      pm.dispose();
    //      flipped.dispose();
    //      tx.dispose();
            m_fbo.dispose();
            m_fbo = null;
            spriteBatch.dispose();
    //      return texture;
            return pm;
        }
    //---------------------------
    }//class BaseBulletTest
    //---------------------------

4 个答案:

答案 0 :(得分:12)

声明类似函数的问题:

type Foo 
  = Bar 
  | Baz

enumFoo = 
  [ Bar 
  , Baz ]

是你可能会忘记添加新的枚举。为了解决这个问题,我一直在玩这个(hacky,但比上面的想法更少hacky)的想法:

enumFoo : List Foo
enumFoo =
  let
    ignored thing =
      case thing of
          Bar ->  ()
          Baz -> ()
          -- add new instances to the list below!
  in [ Bar, Baz ]

这样你至少会得到一个函数错误,希望不要忘记将它添加到列表中。

答案 1 :(得分:9)

不幸的是,没有。这是不可能的。

对于像Color类型一样具有有限数量值的简单类型,编译器应该能够生成这样的列表。然而,就编译器而言,你的类型和类型

之间没有区别
type Thing = Thing String

要迭代Thing类型的所有值,则需要迭代String类型的所有值。

答案 2 :(得分:3)

当然,你可以做到。只是不自动通过编译器。

.row{
  width: 1360px;  
}

.container{
width: 1360px;    
}

.col-md-4 img {
  -webkit-transition: all 1s ease;
     -moz-transition: all 1s ease;
       -o-transition: all 1s ease;
      -ms-transition: all 1s ease;
          transition: all 1s ease;
          padding: 2px;
          position: relative;
}

.col-md-4 img:hover {
  -webkit-filter: blur(2px);
   -moz-filter: blur(2px);
    -o-filter: blur(2px);
      -ms-filter: blur(2px);
          filter: blur(2px);    
}

.col-md-4 p{

    -webkit-transition: all 0.9s;
  -moz-transition: all 0.9s;
  -ms-transition: all 0.9s;
  -o-transition: all 0.9s;
  transition: all 0.9s;
  visibility: hidden;
  position: absolute;

  top: 175px;
  left: 60px;
  text-decoration: none;
  font-size: 60px;
  opacity: 0;
  color: #fff;
  font-family: 'Raleway';   
}

.col-md-4:hover p{
  opacity: 1;
  visibility: visible;
  -webkit-transition: all 0.9s;
  -moz-transition: all 0.9s;
  -ms-transition: all 0.9s;
  -o-transition: all 0.9s;
  transition: all 0.9s;    
}

这很好用,但如果编译器一直支持枚举,那么显然会检查更好和穷举。

type Foo 
   = Bar 
   | Baz
   | Wiz

-- just write this for types 
-- you wish to use as enumerations 
enumFoo = 
   [ Bar 
   , Baz
   , Wiz ]

答案 3 :(得分:0)

这不能完全回答您的问题,但是由于Elm与Haskell非常相似,所以我可能不是唯一想知道故事中Haskell方面是什么的人。

在Haskell中(显示ghci)you can do

library(dplyr)
library(purrr)
library(stringr)
data %>% 
   mutate(Words = map_chr(str_extract_all(Text, keywords), ~
        unique(.x) %>%
           sort %>% 
           str_c(collapse = " + ")))

也许榆木的未来版本将从中借鉴。

编辑:我发现Getting a list of all possible data type values in Haskell也会回答这个问题。

相关问题